I have the following HTML for an ngRepeat element:
<ol id="animationFrame">
<li ng-repeat="animationImage in animationImages" ng-repeat-listener>
<img ng-src="{{animationImage.src}}" id="{{animationImage.id}}">
</li>
</ol>
Within which I want to select based on id using angular.element like this:
angular.element('#animation-image-' + Math.floor($scope.animation.frameImage));
Where $scope.animation.frameImage is rounded down to be an integer. I can see that I am using the correct selector ('#animation-image-1' is the initially loaded selector, for example) and when I change $scope.animation.frameImage using a slider, it works perfectly.
The problem is when the page loads, the angular.element object returned using a correct selector is undefined. Now, I assumed this was because the ng-repeat hasn't populated yet. So I added the following directive for ng-repeat-listener included in the HTML above:
.directive('ngRepeatListener', function() {
return function(scope) {
if(scope.$last) {
scope.$emit('NGREPEATCOMPLETE');
}
}
and I use it thusly:
$scope.$on('NGREPEATCOMPLETE', function() {
console.log(angular.element('#animation-image-1'));
};
which is in fact being called at some point (who knows if it is at the right time or not).
Why isn't this working? How can I fix it?