AngularJs - Error: 10 $digest() iterations reached. Aborting

∥☆過路亽.° 提交于 2019-11-28 07:45:01

Angular performs a digest function to update the DOM when your data changes.

During the digest, it recomputes all the values you have bound in the DOM, in this case {{RandomColorClass()}}. If any of them change, it again performs a digest cycle (since some variables may depend on the value of of the changed variable, for example).

It does this repeatedly until two digests in a row result in the same values -- i.e, nothing has changed.

What's happening is that when a digest occurs, your RandomColorClass() function is being called and returns a different value. This triggers an additional digest, where RandomColorClass() again returns a different value, which triggers another digest...

Can you see where this is going? You shouldn't be generating random values in this manner -- instead, generate them in your scope and persist them.

One approach might be, in your scope:

function randomColourClass() { /* ... */ };

$scope.GridStockRecords.forEach(function(record) {
  record.colorClass = randomColourClass(); 
});

and HTML:

    <div ng-repeat="stockRecord in GridStockRecords | filter:searchText"
         ng-class="stockRecord.colorClass">
      <div>
        <h6>{{stockRecord.ProductGroupName}}</h6>
      </div>
    </div>

I had the same problem in IE10 turns ut that the problem was that I was redirecting using window.location.

window.location = "#route/yada";

Changed the code to

$location.path("/route/yada);

And that solved my issues. =D

Answer unrelated to this particular question, but I'm adding it here because it's on the Google front page when you search for the error message and it took me a bit until I figured it out:

I had something like this in my view:

<custom-tag data="[1,2,3]"/>

And the controller of the custom tag had a watcher set up on $scope.data. This caused AngularJS to barf because every time it re-checked the value of data it got a new object from the view (remember, the array is an object) so it never finished digesting it properly.

I encountered this error when I mistyped ng-class="submit()" instead of ng-click="submit()". I can't imagine anyone else making such a silly error, but for the record, this is another way to create 10 $digest() iterations reached aborting!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!