AngularJS - Is it clean to use $ timeout to execute function after ng-repeat

I am using a directive ngRepeat

to display a large list of data, and I am using $timeout

to execute the function after ng-repeat

if it is completed. It works well, but is this the correct way?

I mean, will it work for all cases?

+3


source to share


2 answers


You can try $last

($ last = true if the repeated item is the last in the iterator).

<div ng-repeat="item in data">
    <span ng-if="$last === true" ng-init="doSomething()"></span>
</div>

      



Example: http://jsfiddle.net/by3qk8w5/2/

+3


source


I can't see your code, but I am assuming that you just set the timeout to an arbitrary number to wait, which would mean that you are basically just hoping the ng-repeat will finish faster.

The correct solution would be to use the last special ng-repeat element property, which is true when the last ng-repeat element list element is displayed. When this becomes true, you execute your callback expression.



A comprehensive solution using a custom directive can be found here, for example: http://www.bennadel.com/blog/2592-hooking-into-the-complete-event-of-an-ngrepeat-loop-in-angularjs.htm

+1


source







All Articles