Tony
Tony

Reputation: 12695

AngularJS - can't read the URL parameter

in my stateProvider definition I have:

return function ( $stateProvider ) {
    return $stateProvider.state('welcomeCAReg', {
                                url:         '/myURL/:id',
                                templateUrl: 'hi.html',
                                controller:  'hiController'
                         })

and then, in the controller I want to read the :id param:

$locationProvider.html5Mode().enabled = true;
....
var paramValue = $location.search().id;

but the paramValue value is undefined, why ?

Upvotes: 1

Views: 35

Answers (2)

Tony
Tony

Reputation: 12695

Ok, I ended up by using

$state.params['id'];

which works

Upvotes: 0

dfsq
dfsq

Reputation: 193261

You should use $stateParams service:

function hiController($stateParams) {
    console.log( $stateParams.id )
}

Upvotes: 1

Related Questions