I have an error when changing the browser url using pushState () in angular app

I need to change browser url when bootstrap modal is open in angularJs app, i used function pushState()

, url changed but it throws the following error:

Uncaught Error: [$rootScope: infdig] 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"]
    ] 
    http: //errors.angularjs.org/1.2.13/$rootScope/infdig?p0=10&p1=%5B%5B%22fn%3…2fn%3A%20%24locationWatch%3B%20newVal%3A%2012%3B%20oldVal%3A%2011%22%5D%5D angular-1.2.13.js:78
    (anonymous function) angular-1.2.13.js:78
    Scope.$digest angular-1.2.13.js:11937
    (anonymous function) angular-1.2.13.js:12084
    completeOutstandingRequest angular-1.2.13.js:4144
    (anonymous function)

      

+3


source to share


1 answer


This is a service bug $location

in angular. It was only fixed in the newest Angular 1.3.0-beta.20, 28/27/2014. See Question 3924 and Duplicate 6976 , which was credited for correction.

The interim fix documentation was made by the angular-ui team: https://github.com/angular-ui/ui-router/issues/562#issuecomment-31623003



The problem is caused by Angular not being synchronously notified of changes to the browser url and trusting its cached version of the url inside $ browser.url (). Since $ browser.url () as a getter is idempotent, it keeps returning the true url without updating its internal record of what the url is.

To make sure this is true, add this line to the provided test just after the window.history.replaceState line and the test will pass: angular.element ($ window) .triggerHandler ('popstate');

This problem can be easily fixed with one line inside $ browser, by resetting lastBrowserUrl inside url-getter: lastBrowserUrl = newLocation || location.href.replace (/% 27 / r, "'"); But it can cause unwanted side effects.

+3


source







All Articles