Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS ui-router, how to get the name of the targeted state in the resolve object of an abstract partent state?

Tags:

angularjs

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');
            }]
        }
    });
like image 570
Dany D Avatar asked Oct 29 '14 14:10

Dany D


1 Answers

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

like image 171
ddhp Avatar answered Sep 29 '22 10:09

ddhp