我試圖通過角度的引導模態在角砌石畫廊中點擊的圖像的網址。我所擁有的與文檔中的演示非常接近,只需進行一些更改。我知道這對我自己對範圍的理解完全是一個問題。
我的HTML模式:
<div class="modal-header">
<h3 class="modal-title">I'm a modal!</h3>
</div>
<div class="modal-body">
<ul>
<li ng-repeat="item in items">
<a href="#" ng-click="$event.preventDefault(); selected.item = item">{{ item }}</a>
</li>
</ul>
</div>
<div class="modal-footer">
<button class="btn btn-primary" type="button" ng-click="ok()">OK</button>
<button class="btn btn-warning" type="button" ng-click="cancel()">Cancel</button>
</div>
在我的控制器:
angular.module('testApp').controller('GalleryCtrl', function ($scope, $modal, $log) {
$scope.animationsEnabled = true;
$scope.items = ['item1', 'item2', 'item3'];
// Temporary Generation of images to replace with about us images
function genBrick() {
var height = ~~(Math.random() * 500) + 100;
var id = ~~(Math.random() * 10000);
return {
src: 'http://placehold.it/' + width + 'x' + height + '?' + id
};
};
this.bricks = [
genBrick(),
genBrick(),
genBrick(),
genBrick(),
genBrick(),
genBrick()
];
this.showImage = function(item){
alert(item.src); // gives me exactly what im trying to pass to the modal
var modalInstance = $modal.open({
animation: $scope.animationsEnabled,
templateUrl: 'views/modal.html',
scope: $scope,
controller: 'GalleryCtrl',
resolve: {
items: function() {
return $scope.items;
}
}
});
modalInstance.result.then(function (selectedItem) {
$scope.selected = selectedItem;
}, function() {
$log.info('Modal dismissed at: ' + new Date());
});
}
});
最後爲了清楚起見,這是我的磚石代碼
<div masonry>
<div class="masonry-brick" ng-repeat="brick in gallery.bricks">
<img ng-src="{{ brick.src }}" alt="A masonry brick" ng-click="gallery.showImage(brick)">
</div>
</div>
現在這工作正常,返回項目對象3將li標籤中的值賦給模態,就像在原始示例中那樣。什麼id真的要傳入模型是(item.src),但無論我改變它似乎永遠不會傳入,以便我可以顯示它。
你試過設置,爲$範圍變量? $ scope.obj = item.src; –