Angular UI router - access state params in parent Angular UI router - access state params in parent angularjs angularjs

Angular UI router - access state params in parent


In the UI-Router documentation here they explain that

"the $stateParams object will only contain the params that were registered with that state."

You can only have access to parent state parameters in a child state using the resolve property on the parent.

Put this on your 'project' state (parent):

...    resolve: {    // Expose projectId parameter to child states    projectId: ['$stateParams', function ($stateParams) {        return $stateParams.projectId;    }]},

Then inside your controller for your state 'project.task' (child) you should have access to both

$stateParams.projectId

and

$stateParams.taskId


I have noticed $state.params contains the child state params. Can't seem to find any documentation on it. It just works


How are you trying to access the taskId? It should be like this, taken from official documentation:

$stateProvider    .state('contacts.detail', {        url: "/contacts/:contactId",        templateUrl: 'contacts.detail.html',        controller: function ($stateParams) {            // If we got here from a url of /contacts/42            expect($stateParams).toBe({contactId: 42});        }    })