使用AngularJS获取JSON文件:
{
"albumId": 1,
"id": 1,
"title": "accusamus beatae ad facilis cum similique qui sunt",
"url": "http://placehold.it/600/92c952",
"thumbnailUrl": "http://placehold.it/150/92c952"
},
并使用ng-repeat显示:
<div ng-repeat="image in images" class="col-xs-2 ">
<div class="thumbnail">
<img src="{{ image.thumbnailUrl }}" alt="" ng-click="open()" />
</div>
</div>
有效。比我已经将ui.bootstrap连接到缩略图以打开模态窗口。
控制器:
app.controller("page3Ctrl",['$scope', '$resource', '$uibModal', function ($scope, $resource,$uibModal) {
var postingsResource = $resource('http://jsonplaceholder.typicode.com/photos', {});
$scope.images = postingsResource.query();
$scope.open = function() {
var uibModalInstance = $uibModal.open({
animation: true,
templateUrl: 'modal.html'
});
};
}]);
工作正常。现在,我怎样才能从thumbnailUrl中显示模态网址中的特定图像?他们应该匹配。
答案 0 :(得分:1)
您可以向open
函数添加一个参数,并将其传递给图像url
。像这样:
$scope.open = function(imageUrl) {
var uibModalInstance = $uibModal.open({
animation: true,
templateUrl: 'modal.html',
controller: ModalInstanceCtrl,
resolve: {
imageUrl: function () {
return imageUrl;
}
}
});
}
所以:
<img src="{{ image.thumbnailUrl }}" alt="" ng-click="open(image.url)" />
该参数也必须传递给模态控制器:
var ModalInstanceCtrl = function ($scope, $modalInstance, imageUrl) {
$scope.imageUrl = imageUrl;
};
有关将值传递给模态控制器的更多详细信息:Pass parameter to modal
答案 1 :(得分:1)
如果您没有为modal
使用任何控制器,那么您只需将$scope
指定为模式的范围,并将所选图像分配给scope
变量并访问它在模态模板中。
var app = angular.module('app', ['ngResource','ui.bootstrap']);
app.run(function($templateCache){
$templateCache.put('modal.html', '<div><a ng-click="$close(true)" class="pull-right">× close</a><img ng-src="{{currentImage.url}}"/></div>');
});
app.controller("page3Ctrl", ['$scope', '$resource', '$uibModal', function($scope, $resource, $uibModal) {
var postingsResource = $resource('http://jsonplaceholder.typicode.com/photos', {});
$scope.images = postingsResource.query();
//no controller for modal
$scope.open = function(image) {
$scope.currentImage = image;
var uibModalInstance = $uibModal.open({
animation: true,
scope:$scope,
templateUrl: 'modal.html'
});
};
/*
//modal has controller
$scope.open = function(image) {
var uibModalInstance = $uibModal.open({
animation: true,
templateUrl: 'modal.html',
resolve: {
image: function(){
return image;
}
},
controller: function($scope, image){
$scope.currentImage = image;
}
});
};
*/
}]);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.11/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-resource/1.5.11/angular-resource.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/2.5.0/ui-bootstrap.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/2.5.0/ui-bootstrap-tpls.min.js"></script>
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">
<div ng-app="app" ng-controller="page3Ctrl">
<div ng-repeat="image in images.slice(0,10)" class="col-xs-2 ">
<div class="thumbnail" ng-click="open(image)">
<img ng-src="{{ image.thumbnailUrl }}" alt="" ng-click="open()" />
</div>
</div>
</div>
&#13;