我正在尝试制作类似Facebook的模态窗口-例如,您单击图像并在模态窗口中打开,并且URL从/
更改为/img/dj27s_D
,而无需重新呈现视图,而当您关闭模态时,URL返回到/
。
我发现使用
var stateObj = { foo: "bar" };
history.pushState(stateObj, "page 2", "bar.html");
只会使应用程序在下一个
$digest
崩溃。我也尝试过$location.path = '';
但是什么也没发生。如果我执行
$location.path('/img/id')
$ routeProvider将启动并更改视图。我不希望发生这种情况,只想在打开模式时临时更改URL。
最佳答案
您可以将路由器设置为使用先前的路由。例如,在您的控制器中以这种方式:
lastRoute = $route.current;
$scope.$on('$locationChangeSuccess', function (e) {
if (window.location.href.match(/\/view/) || window.location.href === '/') {
$route.current = lastRoute;
}
});
如果
$route.current
不变,则不会触发整页刷新。您可能希望进行一些正则表达式检查,以保持URL空间整洁以防止刷新,但这是一个很小的代价。关于javascript - 不执行routeprovider的 Angular 更改网址,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22134321/