繁体   English   中英

从Angular.js中的URL获取路由参数

[英]Get Route Parameter from URL in Angular.js

如何从以下网址获取我的ng-controlleraccess_token值为$routeParams

http://www.example.com/#/access_token=asdfjaksjdhflanblasdfkjahdloahds

在您的应用程序配置上

App.config(['$routeProvider', function($routeProvider) {
  $routeProvider.
  when('/:id', {
    ....
    controller: 'DetailsController'
  }).

在控制器上使用$ routeParams

.controller("DetailsController", ['$routeParams',function($routeParams){    
    var id = $routeParams.id; // this name is from config :id
}]);

如果未在您的路线中明确定义它,则它将在$routeParams

如果您的路线定义为:

.when('/access_token/:token')

然后,您将可以像这样访问它:

var token = $routeParams.token;

但是,您定义的URL并不是很好的路由参数,也不是有效的查询字符串参数。 如果它是有效的查询字符串参数

#/?access_token=blahblahblah

然后,您可以通过$location.search()方法访问它。

var accessToken = $location.search('access_token');

就目前而言,如果整个键/值对都位于路由参数中,则仍然必须解析该值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM