I feel like most of my Angular questions I ask are me missing something incredibly dumb, so let's hope this is too.
I'm building a directive to control scrolling of a wide list of thumbnails. In jQuery, I had this working perfectly, but the jQuery wasn't comfortable with grabbing a dynamically loaded list from Angular, so I'm rewriting my code. Basically, I've got a gallery that exists in an ng-repeat:
<div id="gallery">
<ul id="gallery_img">
<li class="thumb" ng-repeat="image in thumbnails">
<img ng-src="{{image.thumbnail}}" alt="{{image.description}}" />
</li>
</ul>
</div>
and when I click on some further-down buttons,
<slider class="mover" style="left:0;">«</slider>
<slider class="mover" style="right:0;">»</slider>
the list is supposed to animate/slide backwards and forwards the width of the image in my "0" position. Basically, it grabs the width, then adds that width to my UL left position. Super simple, right?
Here's the directive I've got controlling this movement at the moment:
app.directive('slider', function() { //to reuse, replace "#gallery_img" with whatever you want
return {
restrict: 'E',
link: function(scope, element, attrs){
var toMove = angular.element('#gallery_img');
var imageWidth;
var imageNumber = 1;
element.bind('mousedown', function(){
toMove.css({left: '-=200'});
console.log(toMove.children([imageNumber]).width());
console.log(imageNumber);
imageNumber +=1;
});
}
}
});
I added in some console.log details, so that I could see what widths I'm getting. My imageNumber is definitely changing; each time I click next or previous, the number goes up one. The width I'm getting, though, is only for the first image in each set; I've got several galleries, but no matter how many times I click "next," gallery one will only ever give me "566," gallery two only "433", gallery three "521." Each image is of an entirely different size, so... why is it only giving me the width of the first image?
e: Here's a plnkr of what I'm talking about: http://plnkr.co/edit/nLG4OKsNGvYTgEMg9UOv