angularjs - 在 Angular 应用程序中使用 PushState() 更改浏览器 URL 时出现错误

标签 angularjs pushstate

当在 angularJs 应用程序中打开引导模式时,我需要更改浏览器 URL,我使用了 pushState() 函数,URL 已更改,但它引发以下错误:

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)

最佳答案

这是 Angular 中 $location 服务的一个错误。它刚刚在 2014 年 8 月 27 日的最新版本 Angular 1.3.0-beta.20 中修复。请参阅问题 3924和重复的 6976 ,这已归功于修复。

临时修复的文档由 angular-ui 团队制作:https://github.com/angular-ui/ui-router/issues/562#issuecomment-31623003

The problem is caused because Angular isn't synchronously notified of changes to the browser's url, and trusts 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 verify that this is true, add this line in the provided test just after the window.history.replaceState line, and the test will pass: angular.element($window).triggerHandler('popstate');

This issue could be easily fixed with one line inside of $browser , by resetting lastBrowserUrl inside the url getter: lastBrowserUrl = newLocation || location.href.replace(/%27/g,"'"); But this could cause unwanted side effects.

关于angularjs - 在 Angular 应用程序中使用 PushState() 更改浏览器 URL 时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25539501/

相关文章:

angularjs - 如何让用户使用 Express 和 oAuth2 在 Angular 中的多个设备上登录?

javascript - BackboneJS 删除背面的旧 View ?

javascript - 页面刷新后保留pushState历史记录

javascript - 使用pushState时如何出现 "browser back"?

javascript - HTML5 历史 pushState 链接返回

java - 将 AngularJs 对象映射/导航到 Hibernate 实体

javascript - 表刷新时保持行下拉

angularjs指令无法获取以 "-start"结尾的属性

javascript - 使用 ng-repeat 和嵌套循环添加行

javascript - 如何避免在非 IE 浏览器中使用 pushState 和 $.ajax(hash) 进行双重提交?