问题描述
如果我有一个特定的路由参数,该参数是始终作为查询字符串被传递,有没有使用检索参数之间的角的 $ routeParams $ C $的差异C>服务,并利用其
$位置。$$搜索
服务?如果是这样,一个preferable其他?
If I have a specific route parameter that is always being passed as a query string, is there a difference between retrieving the param using angular's $routeParams
service and using its $location.$$search
service? If so, is one preferable to the other?
网址: //本地主机:80 /应用程序/配置文件ID = 42
$ routeParams
办法:
app.controller('ProfileController', ['$routeParams', function($routeParams) {
console.log($routeParams.id);
}]);
$位置
办法:
app.controller('ProfileController', ['$location', function($location) {
console.log($location.$$search.id);
}]);
我已经注射 $位置
在需要执行的问题详细的行为控制器的依赖。此外,该控制器模块还没有对依赖 ngRoute
。
I am already injecting $location
as a dependency in the controller that needs to perform the behavior detailed in the question. Additionally, the controller's module does not yet have a dependency on ngRoute
.
推荐答案
如果您的路由都属于这种类型的:
If your route is of this type:
when('page/:id')
然后使用$位置搜索不会给你在搜索结果中的ID。但是$ routesParams一样。
then using $location search won't give you the ID in the search result. But $routesParams does.
但在搜索PARAMS前的情况:
But in case of search params ex:
when('page?key=value&key2=value2')
在这种情况下,你可以去任何的$ location.search或$ routeParams。
In this case you can go for any of $location.search or $routeParams.
这篇关于越来越URL查询字符串$ routeParams VS $位置之间的差。$$搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!