如何使用角度JS来获取URL参数 [英] How to get the url parameters using angular js
问题描述
HTML源$ C $ C
HTML source code
<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>
Angular.js源$ C $ C
Angular.js source code
<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>
下面的变量位置
和 LOC1
都返回的测试的作为结果的上述网址。这是正确的方法是什么?
Here the variables loc
and loc1
both return test as the result for the above URL. Is this the correct way?
推荐答案
我知道这是一个老问题,但我花了一些时间给稀疏角文档排序了这一点。在 RouteProvider 和 routeParams 是要走的路。路线线了URL到您的控制器/视图和routeParams可以传递到控制器。
I know this is an old question, but it took me some time to sort this out given the sparse Angular documentation. The RouteProvider and routeParams is the way to go. The route wires up the URL to your Controller/View and the routeParams can be passed into the controller.
查看角种子项目。在app.js中,你会发现为路由提供一个例子。要使用PARAMS只是追加他们是这样的:
Check out the Angular seed project. Within the app.js you'll find an example for the route provider. To use params simply append them like this:
$routeProvider.when('/view1/:param1/:param2', {
templateUrl: 'partials/partial1.html',
controller: 'MyCtrl1'
});
然后在您的控制器注入$ routeParams:
Then in your controller inject $routeParams:
.controller('MyCtrl1', ['$scope','$routeParams', function($scope, $routeParams) {
var param1 = $routeParams.param1;
var param1 = $routeParams.param2;
...
}]);
这篇关于如何使用角度JS来获取URL参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!