13

This is a common problem:

5 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: []

The documentation (https://docs.angularjs.org/error/$rootScope/infdig) explains what NOT to do to avoid the situation.

However, once in a while, you end up in the situation anyway. And for a project larger than a toy app, finding the cause of the infinite digest loop can be really hard.

I'm looking for debugging hints on how I can find out where the code is located which causes the loop.

H.B.
  • 142,212
  • 27
  • 297
  • 366
Markus Johansson
  • 3,653
  • 7
  • 35
  • 53

4 Answers4

11

question was already answered here - How to Troubleshoot Angular "10 $digest() iterations reached" Error

as a resume of two best answers:

  1. remove all suspicious HTML - basically remove all your html from the template, and check if there are no warnings (https://stackoverflow.com/a/17129274/274500)
  2. usually it happens when function returns an new object (https://stackoverflow.com/a/17116322/274500)

plus

you can always put conditional breakpoint here https://github.com/angular/angular.js/blob/c3220325a0a95484724a03bedca24e1bbf05dc80/src/ng/rootScope.js#L815 to check the exception stacktrace.

Tereza Tomcova
  • 4,659
  • 3
  • 31
  • 28
Stepan Suvorov
  • 23,436
  • 25
  • 100
  • 172
  • In my case the problem was that there was no infinite loop at all, as i made sure to always return the same object reference. I have detailed how to debug that in [an answer on the linked question](http://stackoverflow.com/a/38981431/546730). This case is quite rare though... – H.B. Aug 17 '16 at 15:57
2

There can be multiple reasons to it. One of the common reason I've faced is due to a filter in the ng-repeat which returns a new array to work on the ng-repeat.

So check your ng-repeat expressions which have associated filters which is modifying the array.

Shashank Agrawal
  • 23,503
  • 10
  • 80
  • 110
0

For me the error was happening in a method changing the url:

  self.setURL = function(item) {
    var mainUrl = window.location.origin + '#/' + self.baseUrl;
    if (item) {
      mainUrl += item.testId;
    }
    $location.$$absUrl = mainUrl;
  };

this was causing the $rootScope:infdig error

So when debugging this check whatever is changing your ur, location, hrefs in your current controller/directive

CommonSenseCode
  • 21,117
  • 30
  • 122
  • 175
0

For me the error was happening in a method changing the url:

> self.setURL = function(item) {
>    
> 
>//Ajax call followed by promise function
>      servicejsfile.somefunctiom(transferObj).then(responce){
>        

> var somethingTrue=responce.featureAvailable;
>if (somethingTrue) {
> dothis();
>}else
> $window.location = newlocation;
> };

This change the last portion of URL of my application. this was causing the $rootScope:infdig error

So when debugging this, I commented out this portion, and error was not repeated along with requirement of application also changed.

I am not able to fix this so we requirement remains same and I can change the location.

This issue is taking place from some a Ajax call inside a funtion of a controller, and only in IE For FF , chrome and safari it is behaving perfect as JavaScript engine is faster for these browsers.

Vishesh
  • 93
  • 2
  • 10