1

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)
Rohit Rai
  • 286
  • 4
  • 13
  • [This might help](http://stackoverflow.com/a/14381445/865175): "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 Sep 22 '15 at 07:47
  • 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 Sep 22 '15 at 07:48
  • 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 Sep 22 '15 at 07:49
  • Added complete code scenario. – Rohit Rai Sep 22 '15 at 07:58
  • When digest cycle limit is reached the error code i believe also highlights the culprit code. Do you get any such details? – Chandermani Sep 22 '15 at 08:00
  • Added complete error trace, all errors are pointing to angular.js file. – Rohit Rai Sep 22 '15 at 08:10
  • What is `$locationChangeSuccess`? Reads like "on location change -> change location -> on location change -> change location ..." – Iulian Onofrei Sep 22 '15 at 08:43

1 Answers1

0

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.

Joy
  • 9,430
  • 11
  • 44
  • 95
  • Thanks Joy for pointing this out. I missed some lines of code in question. location.reload() is not getting called always, only when some condition meets. Once validateAuthToken method called, location.reload() will not be called again. – Rohit Rai Sep 22 '15 at 09:39
  • Em. Then you probably need to show more code, especially the condition on which you call `reload`. Your error seems there is some infinite digest cycle. – Joy Sep 22 '15 at 12:56