Get Route Parameter from URL in Angular.js

自闭症网瘾萝莉.ら 提交于 2019-12-25 00:09:53

问题


How can I get the value of access_token in my ng-controller as $routeParams from the following URL?

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

回答1:


on you app config

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

use on your controller $routeParams

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



回答2:


If it isn't defined explicitly in your route, then it will not be available in the $routeParams

If your route is defined as:

.when('/access_token/:token')

Then you will be able to access it like this:

var token = $routeParams.token;

However, the URL you have defined isn't really a good route param, or a valid query string parameter. If it were a valid query string parameter

#/?access_token=blahblahblah

Then you could access it via the $location.search() method.

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

As it stands right now, you would still have to parse out the value if the entire key/value pair resides in your route param.



来源:https://stackoverflow.com/questions/26100931/get-route-parameter-from-url-in-angular-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!