简体   繁体   中英

Get Route Parameter from URL in Angular.js

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

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

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
}]);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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