AngularJS UI-Router Access $stateParams from State's data object

Viewed 1381

Given the following state in ui-router:

.state('some.state', {
    url: '/some/:viewType',
    templateUrl: 'myTemplate.html',
    controller: 'SomeStateController',
    controllerAs: 'vm',
    data: {
        authorizedFor: [SOME_ROLE]
    }
}

I'm trying to use the "data" object for a state to help control access to authorized states. Separately, I handle the $stateChangeStart event to look at data.authorizedFor and act accordingly.

The problem, though, is that the list of authorized roles might change based on the value of :viewType. I thought I could let data:{} be a function, inject $stateParams, and handle the logic there...but that won't do.

So, I tried using the params object instead, but at the $stateChangeStart time, the :viewType is not yet accessible from $state.params or $stateParams.

Stepping through in dev tools, I noticed that $state.transitionTo.arguments is populated, but it seems awfully hacky to go that route.

params: {
    authorizedFor: function($state) {
        console.log($state.transitionTo.arguments[1].viewType); // has value I need
    }
}

Any suggestions?

1 Answers
Related