Is this behavior normal? http://plnkr.co/edit/P1TsiIJbV3MvAjHuJRmF?p=preview - two items in collection and ranTimes being called 20 or more times..
1 Answers
Actually it is run exactly 10 times and then aborts with the Error: 10 $digest() iterations reached. Aborting!
(open a console to see the error).
This is the expected behavior since AngularJS relays on dirty-checking of a model to figure out when / which part of the DOM to repaint. I don't want to repeat excellent explanations from Misko here (so be sure to check this post: https://stackoverflow.com/a/9693933/1418796) but in short the model you've got never stabilizes as you've got moving parts that depend on each other.
To see a real number of the run()
method call you could slightly modify your plunker and output results on the console: http://plnkr.co/edit/fYvguOiM2Y2ocOk02yEN?p=preview As you can see this function is run 4 times in total (2 times per each item) which is exactly illustrating dirty-checking at works.
I know that results from your initial plunker might be surprising but this is just showing how AngularJS machinery works under the hood. While it does exactly what we want in wast majority of times we need to be aware of its inner working in some corner cases like this.

- 1
- 1

- 117,202
- 60
- 326
- 286
-
1Is there any way we could limit the the function call inside the ng-repeat to only the no. of times the ng-repeat is looping through. – Zeeshan Jan Aug 01 '15 at 02:24
-
@Jan, you can surely limit it by using ng-init to carry its value. ref: http://stackoverflow.com/a/39408368/1666582 – saurabh Sep 09 '16 at 09:39