Szymon Dzumak
Szymon Dzumak

Reputation: 151

AngularJS getting the image in modal from thumbnail from JSON

Using AngularJS to get JSON file:

{
 "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"
},

and display using 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>

That works. Than I've connected the ui.bootstrap to thumbnails to get modal window opened.

controller:

    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'

        });
    };

}]);

Works fine. Now, how can I possibly go about displaying the particular image in modal url from thumbnailUrl? They should match.

Upvotes: 1

Views: 2157

Answers (2)

ajai Jothi
ajai Jothi

Reputation: 2294

if you are not using any controller for modal, then you can simply assign $scope as modal's scope and assign the selected image to a scope variable and access it in modal template.

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">&times 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;
      }
    });
  };
  */

}]);
<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>

Upvotes: 1

creimers
creimers

Reputation: 5303

You could add an argument to the open function and pass it the image's url. Like so:

$scope.open = function(imageUrl) {
    var uibModalInstance = $uibModal.open({
        animation: true,
        templateUrl: 'modal.html',
        controller: ModalInstanceCtrl,
        resolve: {
           imageUrl: function () {
             return imageUrl;
           }
        }
    });
}

And so:

<img src="{{ image.thumbnailUrl }}" alt="" ng-click="open(image.url)" />

The parameter would also have to be passed to the modal controller:

var ModalInstanceCtrl = function ($scope, $modalInstance, imageUrl) {
    $scope.imageUrl = imageUrl;
};

More details about passing values to modal controller here: Pass parameter to modal

Upvotes: 1

Related Questions