为什么我的 $location.path('test') 不改变地址栏

Why doesn't my $location.path('test') change the address bar

我在庞大的代码库中工作,所以我无法在 fiddle 中复制结果。我正在处理的组件写在 Angular.

到目前为止我已经试过了

window.location.href = 'test'
$location.path('test')
$location.hash('test')

每个变体都有效,console.log 确认哈希已更改,但我的地址栏保持不变。

我知道有很多变数可能导致这种情况,但我没有办法去探索。谁能给我一些我可能忽略的可能原因?我搜索了 preventDefault() 的代码库,我尝试了各种不同的方法来设置哈希值,我什至尝试将哈希值重新定义包装在 setTimeout 中。

感谢您的帮助!

你可以试试

$timeout(function() {    
     $location.path('test');
});

假定所有 angular 操作都必须 运行 在应用程序的摘要周期中。如果您使用不使用某些 angular 服务处理它的函数进行此调用,则不会将更改通知上下文。