I have the following code which repeats and displays the name of the user and his score:
<div ng-controller="AngularCtrl" ng-app>
<div ng-repeat="user in users | orderBy:predicate:reverse | limitTo:10">
<div ng-init="user.score=user.id+1">
{{user.name}} and {{user.score}}
</div>
</div>
</div>
And the corresponding angular controller.
function AngularCtrl($scope) {
$scope.predicate = 'score';
$scope.reverse = true;
$scope.users = [{id: 1, name: 'John'}, {id: 2, name: 'Ken'}, {id: 3, name: 'smith'}, {id: 4, name: 'kevin'}, {id: 5, name: 'bob'}, {id: 6, name: 'Dev'}, {id: 7, name: 'Joe'}, {id: 8, name: 'kevin'}, {id: 9, name: 'John'}, {id: 10, name: 'Ken'}, {id: 11, name: 'John'}, {id: 1, name: 'John'}, {id: 2, name: 'Ken'}, {id: 3, name: 'smith'}, {id: 4, name: 'kevin'}, {id: 5, name: 'bob'}, {id: 6, name: 'Dev'}, {id: 7, name: 'Joe'}, {id: 8, name: 'kevin'}, {id: 9, name: 'John'}, {id: 10, name: 'Ken'}]
}
When I run the above code, I get the Error: 10 $digest() iterations reached. Aborting! error in my console.
I have created jsfiddle for same.
The sort predicate is being initialized only inside the ng-repeat and also the limit is being applied on the number of objects. so I feel having both the sortby and limitTo watchers together is the reason for error.
If the $scope.reverse is false (ascending order of score), then it does not error.
Can anyone help me understand what is wrong here? Much appreciate your help.
For me it was that I was passing a function result as 2-way binding input '=' to a directive that was creating a new object every time.
so I had something like that:
and the controller method on my-dir was
which when I made to
solved the problem!
Hopefully this will help someone :)
The cause of this error for me was...
in my template
It causes the function myTrustSrc in my controller to be called in an endless loop. If I remove the ng-if from this line, then the problem is solved.
The function is only called a few times when ng-if isn't used. I still wonder why the function is called more than once with ng-src?
This is the function in the controller
Please check this jsFiddle. (The code is basically the same you posted but I use an element instead of the window to bind the scroll events).
As far as I can see, there is no problem with the code you posted. The error you mentioned normally occurs when you create a loop of changes over a property. For example, like when you watch for changes on a certain property and then change the value of that property on the listener:
This will result on an error message:
Make sure that your code doesn't have this kind of situations.
update:
This is your problem:
You shouldn't change objects/models during the render or otherwise, it will force a new render (and consequently a loop, which causes the 'Error: 10 $digest() iterations reached. Aborting!').
If you want to update the model, do it on the Controller or on a Directive, never on the view. angularjs documentation recommends not to use the
ng-init
exactly to avoid these kinds of situations:Here's a jsFiddle with a working example.
I have another example of something that caused this. Hopefully it helps for future reference. I'm using AngularJS 1.4.1.
I had this markup with multiple calls to a custom directive:
myData
is an array andWhere()
is an extension method that iterates over the array returning a new array containing any items from the original where the IsOpen property matches the bool value in the second parameter.In the controller I set
$scope.data
like this:Calling the
Where()
extension method from the directive like in the above markup was the problem. To fix this issue I moved the call to the extension method into the controller instead of the markup:and the new controller code:
If you use angular remove the ng-storage profile from your browser console. It is not a general solution bit It worked in my case.
In Chrome F12->Resources->Local Storage
For starters ignore all answers with tell you to use $watch. Angular works off of a listener already. I guarantee you that you are complicating things by merely thinking in this direction.
Ignore all answers that tell you to user $timeout. You cannot know how long the page will take to load, therefore this is not the best solution.
You only need to know when the page is done rendering.
Track the ng-repeat by $index and when the length of array equals the index stop the loop and do your logic.
jsfiddle