Kindzoku Kindzoku - 2 months ago 12
AngularJS Question

ui-router >= 1.0.0 get $transition FROM and TO

In previous versions I've used $rootScope event:

$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams){}


Pretty straightforward approach.

But now I'm forced to use new
$transition
service. So for generic info I'm writing:

$transitions.onStart({}, function(to, from){
console.log(from.router.globals.current.name);
console.log(to.router.globals.current.name);
});


from
is always empty and
to.router.globals.current.name
is it really correct way to get a
state
name now? :D

So, how I can get
from
and
to
names?

Answer

You should be able to get the from and to like this:

$transitions.onStart({}, function(transition){
    console.log(transition.from());
    console.log(transition.to());
});