angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

Infinite loop of $locationWatch

Open JensRantil opened this issue 13 years ago • 31 comments

I sent an initial e-mail to the Google Groups mailing list here: https://groups.google.com/forum/?fromgroups#!searchin/angular/$24locationWatch/angular/Vihjqr04UUw/f3vIBtk9-IIJ I decided to create an issue to get this thing documented properly.

I have two AngularJS applications that individually functions correctly. One of them uses a $routeProvider to switch between views and the other does not. I just ran into a bug when having both applications on a single page; Switching from http://myurl.com/page.html#/one to http://myurl.com/page.html#/two triggers an infinite loop of $locationWatch calls. Initially loading the page works fine.

I am getting errors similar to #1417, but there are some differences:

  • I am having this issue in Chromium, but many mention that that issue is an IE issue.
  • I am not doing any history.pushStates. My location change is simply done by clicking on an <a href="#/two">link</a> tag.
  • my digest cycle does never finish instead a new $digest cycle is initiated when the previous one has been aborted (it says "Aborting!" in the log over and over again). Here's an extract from my console log:
Error: 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: "[[\"fn: $locationWatch; newVal: 8; oldVal: 7\"], [\"fn: $locationWatch; newVal: 9; oldVal: 8\"], [\"fn: $locationWatch; newVal: 10; oldVal: 9\"], [\"fn: $locationWatch; newVal: 11; oldVal: 10\"], [\"fn: $locationWatch; newVal: 12; oldVal: 11\"]]"
    at Error (<anonymous>)
    at Object.Scope.$digest (http://192.168.147.25:8082/js/angularjs/angular-1.0.6.js:7925:19)
    at Object.Scope.$apply (http://192.168.147.25:8082/js/angularjs/angular-1.0.6.js:8097:24)
    at http://192.168.147.25:8082/js/angularjs/angular-1.0.6.js:9420:36
    at completeOutstandingRequest (http://192.168.147.25:8082/js/angularjs/angular-1.0.6.js:3001:10)
    at http://192.168.147.25:8082/js/angularjs/angular-1.0.6.js:3281:7 angular-1.0.6.js:5704
Uncaught Error: 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: "[[\"fn: $locationWatch; newVal: 8; oldVal: 7\"], [\"fn: $locationWatch; newVal: 9; oldVal: 8\"], [\"fn: $locationWatch; newVal: 10; oldVal: 9\"], [\"fn: $locationWatch; newVal: 11; oldVal: 10\"], [\"fn: $locationWatch; newVal: 12; oldVal: 11\"]]" angular-1.0.6.js:7925
Error: 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: "[[\"fn: $locationWatch; newVal: 19; oldVal: 18\"], [\"fn: $locationWatch; newVal: 20; oldVal: 19\"], [\"fn: $locationWatch; newVal: 21; oldVal: 20\"], [\"fn: $locationWatch; newVal: 22; oldVal: 21\"], [\"fn: $locationWatch; newVal: 23; oldVal: 22\"]]"
    at Error (<anonymous>)
    at Object.Scope.$digest (http://192.168.147.25:8082/js/angularjs/angular-1.0.6.js:7925:19)
    at Object.Scope.$apply (http://192.168.147.25:8082/js/angularjs/angular-1.0.6.js:8097:24)
    at http://192.168.147.25:8082/js/angularjs/angular-1.0.6.js:9420:36
    at completeOutstandingRequest (http://192.168.147.25:8082/js/angularjs/angular-1.0.6.js:3001:10)
    at http://192.168.147.25:8082/js/angularjs/angular-1.0.6.js:3281:7 angular-1.0.6.js:5704
Uncaught Error: 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: "[[\"fn: $locationWatch; newVal: 19; oldVal: 18\"], [\"fn: $locationWatch; newVal: 20; oldVal: 19\"], [\"fn: $locationWatch; newVal: 21; oldVal: 20\"], [\"fn: $locationWatch; newVal: 22; oldVal: 21\"], [\"fn: $locationWatch; newVal: 23; oldVal: 22\"]]" 

Some additional information:

  • AngularJS version: 1.0.6.
  • The other app does not use $route, $routeParams or $location directly. It does, however, use AngularUI, that uses $location. I've tried inactivating AngularUI, but am still getting getting the loop.
  • I know there are reported bugs when doing things like this in html5 mode. I am not using html5 mode.
  • Monkeypatching the $locationWatch to print to console, I notice that old value constantly is "http://myurl.com/page.html#/one" and new value constantly is "http://myurl.com/page.html#/two".
  • I also notice that browser location bar is not changing while the loop is running.
  • I am using Chromium 25.0.1364.160 Ubuntu 10.04.

I've tried recreating this issue, but have so far failed: http://plnkr.co/edit/YSAHIocwCePqg8ZZig11

JensRantil avatar May 29 '13 08:05 JensRantil

Faced the same issue when using window.history API with angular routing. in angular v1.2 rc3

oilart avatar Dec 04 '13 10:12 oilart

Below commit may be the solution If you are faced the same issue when using history api in angular v1.2.x

https://github.com/angular/angular.js/pull/5425

kimwz avatar Dec 16 '13 08:12 kimwz

I am getting this on the latest version of angularjs also when I use ng-include in my code

chaudhary avatar Feb 27 '14 08:02 chaudhary

@chaudhary can you make a plunkr or jsfiddle in a new issue demonstrating your problem? Thanks!

btford avatar Feb 27 '14 10:02 btford

I have exactly the same issue listed here. I am using AngularJS 1.2.10. I tried to create a plunkr with a code snippet that I have in my project: http://plnkr.co/edit/1jLBAuwvl8RIDKrVFvjS

but am not able to see any of the text box from the ng-include src. I see the looping problem in chrome

kavuri avatar Apr 18 '14 07:04 kavuri

@kavuri is there a chance you are bootstrapping your app twice? both programatically and through use of ng-app, perhaps? upgrading to v1.2.15 will help to catch that if it's potentially the problem.

notatestuser avatar Apr 18 '14 09:04 notatestuser

I have the same issue. My code before fix ng-include="'partials/account/navbar-login'" after ng-include="'/partials/account/navbar-login'" as we can see i forget "/" before partials. It help me.

Ilya-Valasiuk avatar Apr 20 '14 10:04 Ilya-Valasiuk

@Scoolboy true, when I had "/" before the directory, it works. Interestingly, if I have a subdirectory under "/partials", like "/partials/graphs/graph.html", then I get errors. Did you happen to face any such problem?

kavuri avatar Apr 20 '14 12:04 kavuri

@kavuri no i dont have any problems with this case. Try this on your router app.get('/partials/*', function(req, res) { res.render('../../public/app/' + req.params); });

Ilya-Valasiuk avatar Apr 20 '14 13:04 Ilya-Valasiuk

I also experienced this. Complicated to figure out what happens.

related to adding this:

$rootScope.$on('$routeChangeSuccess', function() {
  var path = $window.location.pathname + '#' + $location.path();
  if(typeof $window.ga !== 'undefined') {
    $window.ga('send', 'pageview', {
      page: path
    });
  }
});

sorry I can't isolate it more. dragons be here.

crucialfelix avatar Jun 04 '14 17:06 crucialfelix

Version: 1.2.2 I experienced this issue when having a jQuery click listener that compiled and added angular markup. None of the proposed changes worked for me, and the issue occured even though I wasn't calling $location at all.

I noticed that if I entered the page with a hash sign and clicked the element the hashsign got removed, and vide versa. (http://localhost# <-> http://localhost)

Finally I tracked down the issue which was that the click event bubbled up to $rootElement and triggered a click listener defined in http://ajax.googleapis.com/ajax/libs/angularjs/1.2.2/angular.js line 8928. It is unclear to me what that click listener does. The solution for me was to add event.preventDefault() in my own click listener.

dignite avatar Nov 10 '14 15:11 dignite