2

I am trying to have a grid of images, when clicked the image is hid from the grid and shown featured at the top.

HTML:

    <div ng-controller="imageCtrl">
      <div class="row">
        <img ng-show="selected" src="{{selected.img}}" alt="">
        <p ng-show="selected">{{selected.des}}</p>
      </div>
      <div class="row">
        <div ng-repeat="(id, image) in images" class="col-sm-4">
          <a ng-click="clicked(id)"><img src="{{image.img}}" alt=""></a>
        </div>
      </div>
    </div>

And my controller:

light.controller('imageCtrl', function($scope){
  $scope.images = [{img: '',des: ''},{img: '',des: ''},{img: '',des: ''}];
  $scope.selected = $scope.images[0];
  $scope.clicked = function(id){
    selected = $scope.images[id];
};

});

Currently the first image is shown but when I click on other images nothing happens. Anybody have any tips as to what I'm doing wrong? Thank You!

4
  • I don't think you can use (x,y) in z syntax in ng-repeat - instead just use ng-repeat="image in images" and then use the $index variable, which is available inside an ng-repeat, as the replacement for your current id. Commented May 9, 2014 at 21:04
  • Do I use $index in ng-click="clicked($index)" or is it used in my function? Commented May 9, 2014 at 21:08
  • Use clicked($index) and in your function you can keep id as is. Commented May 9, 2014 at 21:44
  • But, @nilsK's answer is the correct one. Commented May 9, 2014 at 21:44

3 Answers 3

4

you'll need to set $scope.selected not selected

$scope.clicked = function(id){
   $scope.selected = $scope.images[id];
}

EDIT: i suck at plunker ;)

anyways: use ng-repeat as you did in your first example and it should work

<div ng-repeat="(id, image) in images" class="col-sm-4">
   <a ng-click="clicked(id)"><img src="{{image.img}}" alt=""></a>
</div>
Sign up to request clarification or add additional context in comments.

4 Comments

Still no luck any other thoughts? I'm stumped.
Pretty sure. Check it out lightboxtest.myshopify.com click on the image of Richard Sherman
I just don't think my click is being registerd
@user3621884 switch back to your first ng-repeat expression. i edited my post
0

Try this:

In html, instead of passing id pass the index value as

ng-click="clicked($index)"

In the controller,

$scope.clicked = function(Index){
$scope.selected = $scope.images[Index];

};

Comments

-1

Use this instead:

<img ng-show="selected" ng-src="{{selected.img}}" alt="">

Use ng-src={{selected.image}} in your image tag instead of just src={{selected.image}}. Angular will know it needs to update the image if the model its pointing to has been updated

5 Comments

Any thoughts ExpertSys?
Sorry that didn't work, @ExpertSystem I don't know how that could be considered unrelated as that could produce the exact symptoms. Can you verify that the contents of selected are being updated on click?
@TyndieRock: src="{{...}}" will work fine as sson as Angular kicks in and replaces the template. So, it could not produce the symptoms.
I don't think they are. Is their anyway to check for certain?
This is definitely not the issue.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.