如何使用AngularJS获取url参数HTML源代码<div ng-app="">
<div ng-controller="test">
<div ng-address-bar browser="html5"></div>
<br><br>
$location.url() = {{$location.url()}}<br>
$location.search() = {{$location.search('keyword')}}<br>
$location.hash() = {{$location.hash()}}<br>
keyword valus is={{loc}} and ={{loc1}} </div></div>AngularJS源代码<script>function test($scope, $location) {
$scope.$location = $location;
$scope.ur = $scope.$location.url('www.html.com/x.html?keyword=test#/x/u');
$scope.loc1 = $scope.$location.search().keyword ;
if($location.url().indexOf('keyword') > -1){
$scope.loc= $location.url().split('=')[1];
$scope.loc = $scope.loc.split("#")[0]
}
}
</script>这里变量loc和loc1返回测试作为上述URL的结果。这是正确的方法吗?
3 回答
撒科打诨
TA贡献1934条经验 获得超2个赞
function GetURLParameter(parameter) { var url; var search; var parsed; var count; var loop; var searchPhrase; url = window.location.href; search = url.indexOf("?"); if (search < 0) { return ""; } searchPhrase = parameter + "="; parsed = url.substr(search+1).split("&"); count = parsed.length; for(loop=0;loop<count;loop++) { if (parsed[loop].substr(0,searchPhrase.length)==searchPhrase) { return decodeURI(parsed[loop].substr(searchPhrase.length)); } } return ""; }
添加回答
举报
0/150
提交
取消