get parent state from ui-router $stateChangeStart

2019-09-05 23:27发布

问题:

I want to access the name of the parent state when detecting a statechange. My code so far:

app.run(function($rootScope, $state, authProvider) {
    $rootScope.$on("$stateChangeStart", function(event, toState, toParams, fromState, fromParams) {
        console.log(toState.name);
    });
});

Returns [parentState.childState]. One possibility would be to use regex to replace every symbole that comes after the "." but I guess there has to be a function in ui-router to get the name of the parent state.

What would I have to add? to get [parentState]?

Example: The user comes from the state login to the state pets.dogs. pets is an abstract parent state, dogs is a child of it. fromState.name would return login but I want pets.

EDIT: Yes, the workaround toState.name.split(".")[0] is possible but only works under controlled conditions.

回答1:

I think you can do toState.parent.name.