I am using ui-router and I have child states and an abstract one - the parent.
Now, I want to be able to do some user role management and I want to do it in a single place, for all roots.
The best way to do it, is in the parent's state resolve object.$stateProvider
Here, i do this to mange app width authentication data.
The question is, how do I get the targeted state from the resolve function of an abstract state?
.state('section', {
abstract: true,
template: '<ui-view/>',
resolve: {
// Auth & role management middleware.
UserData: ['$q', '$stateParams', 'Session', function ($q, $stateParams, Session) {
// HOW DO I GET THE DESTINATION ROUTE HERE.
$state.current.name - returns null
$stateParams - is an empty object.
// ANY IDEAS?
var userData;
userData = Session.getUserData();
if (userData.token) {
return userData;
}
return $q.reject('login');
}]
}
});
state hasn't updated when inside resolve.
My workaround by far is listen to '$stateChangeStart' event by
$rootScope.$on('$stateChangeStart', callback)
this event is emitted before resolve, and second param passed to callback is toState
which has the info you need.
You can put this logic into your Session service
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With