问题
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