Tahin Rahman
Tahin Rahman

Reputation: 1204

Get Route Parameter from URL in Angular.js

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

Upvotes: 0

Views: 2243

Answers (2)

BlaShadow
BlaShadow

Reputation: 11683

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

Upvotes: 3

Josh
Josh

Reputation: 44906

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.

Upvotes: 2

Related Questions