AngularJS更改URL但不更改视图

时间:2022-10-29 19:39:52

We are using angularJS and angular ui-router and want to change the URL in the address bar but keep the view/state the same.

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

I have tried using history pushstate but I am having conflicts with it causing an infinite $digest loop.

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

I have also tried the method described in sync() https://github.com/angular-ui/ui-router/wiki/Quick-Reference#urlroutersync - which is essentially calling preventDefault on $locationChangeSuccess. However, this doesn't seem to work - as demonstrated by this plunkr: http://plnkr.co/pLC2Ai

我也曾尝试()同步描述的方法https://github.com/angular-ui/ui-router/wiki/Quick-Reference#urlroutersync - 这基本上是在$ locationChangeSuccess调用了preventDefault。然而,这似乎不起作用 - 正如这个plunkr所证明的那样:http://plnkr.co/pLC2Ai

Does anyone know of any solutions to this issue?

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

2 个解决方案

#1


2  

Of course, my mate! :)

当然,我的伴侣! :)

use $stateChangeStart, not $locationChangeSuccess

使用$ stateChangeStart,而不是$ locationChangeSuccess

#2


0  

Have you tried the history.pushState method?

你试过history.pushState方法吗?

On Mozilla's site the say: "This will cause the URL bar to display http://mozilla.org/bar.html, but won't cause the browser to load bar.html or even check that bar.html exists."

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

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

You may need to inject $locationProvider and then set $locationProvider.html5Mode(true).

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

#1


2  

Of course, my mate! :)

当然,我的伴侣! :)

use $stateChangeStart, not $locationChangeSuccess

使用$ stateChangeStart,而不是$ locationChangeSuccess

#2


0  

Have you tried the history.pushState method?

你试过history.pushState方法吗?

On Mozilla's site the say: "This will cause the URL bar to display http://mozilla.org/bar.html, but won't cause the browser to load bar.html or even check that bar.html exists."

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

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

You may need to inject $locationProvider and then set $locationProvider.html5Mode(true).

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