arielorvits
arielorvits

Reputation: 5535

stateParams in url of abstract state, ui-router, angularjs

if i have an abstract state:

$stateProvider.
        state('drug', {
            abstract: true,
            url: '/drug/:drugId',
            template:
            '<ui-view></ui-view>',
            resolve: {
                drugId : ['$stateParams', function($stateParams){
                    return $stateParams.drugId;
                }]
            }
        }

that have some child states, in form: drug.x,drug.y and I want to choose, in one place in the app, a drugId that will come through the abstract state to all the child states, so that after that when i call to 'drug.x' state, it'll have the drugId value - where and how i do this one call to drug state with drugId param? I know that I can't call the abstract state itself.

thanks.

Upvotes: 3

Views: 2582

Answers (1)

Chris T
Chris T

Reputation: 8216

You have already done the tricky part by exposing $stateParams.drugId as a resolve. Now you just inject it into your substate controller like so:

$stateProvider.state('drug.x', {
  controller: function(drugId) { } // drugId is injected from the resolve you defined in 'drug'
}

To provide the parameter to drug.x, you simply add it to the transition parameters:

$state.go('drug.x', { drugId: 123 });

or

<a ui-sref="drug.x({ drugId: scopeVariable })">Go to drug.x for {{ scopeVariable }}</a>

Upvotes: 3

Related Questions