Javascript 更改位置哈希后返回上一个url
我的代码是:Javascript 更改位置哈希后返回上一个url,javascript,angularjs,Javascript,Angularjs,我的代码是: $rootScope.$on("$locationChangeSuccess", function () { if( $location.$$path.startsWith('/room/') ){ $location.hash('container'); $anchorScroll(); }
$rootScope.$on("$locationChangeSuccess", function () {
if( $location.$$path.startsWith('/room/') ){
$location.hash('container');
$anchorScroll();
}
});
在myroom
页面上,视图被滚动到ID为container
的元素所在的位置。但是,在滚动期间,我的URL附加了#容器
,我的后退按钮停止工作
每当我进入example.com/#/room/123
,我都会被重定向到example.com/#/room/123#container
,然后我的后退按钮指向example.com/#/room/123
。因此,每次我回击时,URL都会返回到example.com/#/room/123
,然后运行上述代码并将哈希添加到代码中
有没有办法防止这种情况发生并保持后退按钮正常工作?使用位置。更换(“容器”)代码>取而代之。接下来,我通过替换
$location.hash('container');
与
你用的是内置的角度路由器吗?我用的是不,那不行。“$locationChangeSuccess”
中的代码已运行,但我被重定向回索引页。location.replace
应添加哈希而不更改历史记录。我推测这是您的问题。我不想要哈希(如果可能)。我试过使用location.replace()
,但我再也看不到我的滚动条了。你试过$location.replace了吗?“如果调用,当前$digest期间对$location的所有更改都将替换当前历史记录,而不是添加新记录。”已设法使其与$location.hash('container').replace()一起使用代码>。谢谢
$location.hash('container').replace();