1
votes

I'm using location.reload() / $window.location.reload() function to reload my angular app, it reloads the page but also generates the following error in console:

[$rootScope:infdig] 10 $digest() iterations reached. Aborting!

I'm calling validateAuthToken from run method of app controller which internally calls location.reload() method.

.run(function ($rootScope, $urlRouter, security, $location) {
 
  $rootScope.$on('$locationChangeSuccess', function(e) {
    security.validateAuthToken();  
  });
});

angular.module('security', [])

.factory('security', [ '$http', '$q', '$location', function($http, $q, $location) {

  var service = {
  validateAuthToken: function() {
      var request = $http.post(url, params);
      return request.then(
        function(response) {
          if(CONDITION_MEETS) {
           location.reload();
          }
        },
        function(error) {
        });
  }
  return service;
 }]);

Complete error trace:

Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: []
http://errors.angularjs.org/1.3.8/$rootScope/infdig?p0=10&p1=%5B%5D
at REGEX_STRING_REGEXP (angular.js:63)
at Scope.promises.$get.Scope.$digest (angular.js:14263)
at Scope.promises.$get.Scope.$apply (angular.js:14488)
at done (angular.js:9646)
at completeRequest (angular.js:9836)
at XMLHttpRequest.requestError (angular.js:9787)
1
This might help: "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" - Iulian Onofrei
Please post more code, so we can se whats going on. Like where you call reload() and how your controller initializes. Maybe even a running example. - Andreas
Maybe you set a watch on window and by calling the reload() method on it's location property inside the watch callback it gets in a loop. - Iulian Onofrei
Added complete code scenario. - Rohit Rai
When digest cycle limit is reached the error code i believe also highlights the culprit code. Do you get any such details? - Chandermani

1 Answers

0
votes

Because your code here:

$rootScope.$on('$locationChangeSuccess', function(e) {
    security.validateAuthToken();  
});

The function validateAuthToken will reload the page when the $http.post fails:

function(response) {
    location.reload();
},

Reloading the page triggers another new event $locationChangeSuccess, which will call $http.post again... So come the infinite loop. You'd better just popup a modal saying the initialization is failed, instead of reloading the page.