Angular RouterUI $ location.search()vs $ stateParams

时间:2016-09-30 19:42:12

标签: angularjs angular-ui-router

使用Angular UI路由器 - 您可以使用$stateParams访问参数URL。此外,$location服务还可以访问网址参数:$location.search().param1

我知道有时使用$location.search()检索网址中的值是有好处的。 例如,在不重新加载状态的情况下更改URL。示例摘录:

$state.current.reloadOnSearch = false;
$location.search('param1', 'new value').replace();
$timeout(function () {
    $state.current.reloadOnSearch = undefined;
});

在这种情况下,使用新参数param1更改网址 - 如果我尝试使用$stateParams.param1 - 其未定义。意思是,$stateParams并不知道这种变化。在这种情况下,我使用$location.search().param1来获取更新的值。

如您所见,有时$location.search()优于$stateParams

有哪些缺点?我不能一直使用$location.search()而不能使用$stateParams吗?使用$stateParams有什么好处吗?什么情况?

0 个答案:

没有答案