floydwch / angular-underscore

Underscore adapter for AngularJS
316 stars 86 forks source link

Error: $rootScope:infdig Infinite $digest Loop #16

Open JulianWielga opened 10 years ago

JulianWielga commented 10 years ago

copy-paste of your example: http://plnkr.co/edit/Rr7WBeiwHbCSeQFZ7l19

alexthewilde commented 9 years ago

+1

aolieman commented 9 years ago

The cause is that Angular's digest loop evaluates expressions at least twice, to check if their output is stable. It clearly isn't in the case of shuffle, but other functions with non-deterministic return values, e.g. pairs(), will also cause Angular to get stuck.

The simple solution is to move any expressions with (intermediate, in the case of filters,) non-deterministic return values to your controller (i.e. out of the template). The workaround for this module that comes to mind is to sort the output of non-deterministic functions that are especially useful in templates (such as pairs()). It incurs a bit of overhead, and will be a less clean mapping to underscore, but it will make this module more usable. Currently, I end up shadowing sorted versions of these functions in my controllers.

It would be nice if Angular could take the entire expression into account (i.e. plus filters), so that this issue would not be caused by expressions that sort their output with filters. Perhaps someone would like to upstream this issue there.

EDIT: the workaround isn't as simple as I stated above. For users of this module, ngRepeat's track by clause may help. A true solution will likely involved modified $watch calls.