0

I wrote some code like this:
html:

<li class="js-page-item" ng-repeat="page in pageList">{{page}}</li>

js:

$scope.pageList= [1,2,3,4,5,6];  
var items = $('.js-page-item');  
console.log(items.length); // =>0  

It turns out that when the second line in js code was executed the list hadn't been finished yet. The first thing in my head is using $timeout to defer until the list had been finished. But, I don't think that is a good idea.

So, how can I know when the list has been finished?

nwcat
  • 89
  • 6
  • 1
    Usually the fact that you _want_ to know it indicates that you are approaching the problem from jQuery-angle instead of trying of solving it AngularJS-way. The short answer to your question is that "you can't know", but most of the time you don't need to - there are often better approaches. So: what is the problem you are trying to solve? – pkozlowski.opensource Mar 29 '14 at 16:15
  • @pkozlowski.opensource Hi, I want to use jQuery to do some DOM operation. But, it seemed that the second line jQuery js code was executed before np-repeat finished the DOM structure, and the select engine just got nothing. So, what I try to solve is making the jQuery code being executed after the DOM structure is finished. – nwcat Mar 29 '14 at 17:43

2 Answers2

1

You can use $last , it's a special property that gets true if the repeated element is last in the iterator. Example:

<li class="js-page-item" ng-repeat="page in pageList">{{page}} is last element:{{$last}}</li>

Live example: http://jsfiddle.net/choroshin/FnfQ8/

if you need to invoke an event when ng-repeat has finished, you can follow the example in this stackoverflow answer or this one .

Community
  • 1
  • 1
Alex Choroshin
  • 6,177
  • 2
  • 28
  • 36
0

Use the repeatComplete directive:

http://www.bennadel.com/blog/2592-Hooking-Into-The-Complete-Event-Of-An-ngRepeat-Loop-In-AngularJS.htm

zumalifeguard
  • 8,648
  • 5
  • 43
  • 56