内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用
我查询AngularJS参数的值来读取URL。URL如下:
http://127.0.0.1:8080/test.html?target=bob
如预期,location.search
对应"?target=bob"
.,我在网上找到了很多例子,但是没都没用。:
$location.search.target
$location.search['target']
$location.search()['target']
这些代码有什么用?
更新:
下面是几个问题关于$location
:
什么时候使用$Location? 当前URL被应用程序更改时中的更改作出反应时,浏览器能不能随时做出反应
我的页面是从外部链接打开,所以我的页面不是“对当前URL的变化做出反应”。因此$location不是很合适(对于一些细节问题)。可以使用javascript和正则表达式来搜寻位置。
那么:有没有更好的方法来替换$location
?
你可以:
$location.search().target
搜索“目标”。
你可以参考:http://web.Archive.org/web/20130317065234/http://jsfiddle.net/phnLb/7/
var myApp = angular.module('myApp', []); function MyCtrl($scope, $location) { $scope.location = $location; $scope.$watch('location.search()', function() { $scope.target = ($location.search()).target; }, true); $scope.changeTarget = function(name) { $location.search('target', name); } }
<div ng-controller="MyCtrl"> <a href="#!/test/?target=Bob">Bob</a> <a href="#!/test/?target=Paul">Paul</a> <hr/> URL 'target' param getter: {{target}}<br> Full url: {{location.absUrl()}} <hr/> <button ng-click="changeTarget('Pawel')">target=Pawel</button> </div>
二次