越来越URL查询字符串$ routeParams VS $位置之间的差。$$搜索 [英] Difference between getting URL query string with $routeParams vs $location.$$search

查看:169
本文介绍了越来越URL查询字符串$ routeParams VS $位置之间的差。$$搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个特定的路由参数,该参数是始终作为查询字符串被传递,有没有使用检索参数之间的角的 $ routeParams 服务,并利用其 $位置。$$搜索服务?如果是这样,一个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 $位置之间的差。$$搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆