AngularJS更改URL但不更改视图

时间:2014-01-18 18:35:17

标签: javascript angularjs angular-ui angular-ui-router

我们正在使用angularJS和angular ui-router,并希望更改地址栏中的URL,但保持视图/状态相同。

我尝试过使用历史记录状态,但是我遇到了冲突导致无限的$ digest循环。

我还尝试过sync()https://github.com/angular-ui/ui-router/wiki/Quick-Reference#urlroutersync中描述的方法 - 实际上是在preventDefault上调用$locationChangeSuccess。但是,这似乎不起作用 - 正如这个plunkr所证明的那样:http://plnkr.co/pLC2Ai

有谁知道这个问题的任何解决方案?

2 个答案:

答案 0 :(得分:2)

当然,我的伴侣! :)

使用$stateChangeStart,而不是$locationChangeSuccess

答案 1 :(得分:0)

您是否尝试过history.pushState方法?

在Mozilla的网站上说:“这会导致网址栏显示http://mozilla.org/bar.html,但不会导致浏览器加载bar.html甚至检查bar.html是否存在。”

history.pushState(stateObj, "page 2", "bar.html");

您可能需要注入$ locationProvider,然后设置$ locationProvider.html5Mode(true)。

相关问题