AngularJS ui-router, how to get target state name in partent abstract state resolution object?

I use ui-router, and I have child states, and the abstract one is parent.

Now I want to be able to manage the user role, and I want to do it in one place, for all the roots.

The best way to do this is in the parent state resolution object. $ stateProvider

Here I am doing this in order to process the authentication width of the application.

The question is how to get the target state from the resolution function of the 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'); }] } }); 
+6
source share
1 answer
condition

not updated when allowed internally.

My workaround is definitely listening for the $ stateChangeStart event

$rootScope.$on('$stateChangeStart', callback)

this event is fired before resolution, and the second parameter passed to the callback is toState , which has the information you need.

You can put this logic in your Session service

+2
source

Source: https://habr.com/ru/post/977412/


All Articles