Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get toState param with $transitions service? (new ui-router)

I'm using ui-router 1.0.0-alpha.3. Old events are deprecated there.

so I'm trying to convert

  $rootScope.$on('$stateChangeStart', (event, toState) => {
    //...
  });

to new way of doing things with $transitions.onStart hook -

  $transitions.onStart( {}, function($state, $transition$) {
      //...
  });

where could I get toState param in this case?

like image 285
Stepan Suvorov Avatar asked Jun 13 '16 14:06

Stepan Suvorov


4 Answers

$transitions.onStart({}, function(transition) {
  console.log(transition.params('to').paramname)
})
like image 69
MMM Avatar answered Oct 04 '22 02:10

MMM


Use $transition$.$to() for it.

$transitions.onStart( {}, function($transition$) {
    // stateTo === $transition$.$to();
    // Check $transition$.$to().name for state name
});
like image 21
bolelamx Avatar answered Oct 23 '22 23:10

bolelamx


Follow documents, check Class Transition Methods, toState in old version equal $to() in new version

Old version:

$scope.$on('$stateChangeSuccess', function(evt, toState, toStateParams, fromState) {
  var oldToState = toState;
}

New version (current is 1.0.0-beta.3):

$transitions.onSuccess({}, function($transitions){
  var newToState = $transitions.$to();
}

From Interface HookMatchCriteria:

This object is used to configure whether or not a Transition Hook is invoked for a particular transition, based on the Transition's "to state" and "from state".

Hope this help!

like image 5
Huy Nguyen Avatar answered Oct 24 '22 00:10

Huy Nguyen


$transitions.onSuccess({ }, function(trans) {
    stateChangeSuccessCallBack(**trans.$to().self**, trans.params('to'));
});

trans.$to().self This gives the exact object as in $stateChangeSuccess(event, **toState**)

like image 2
DhiyanEswar Uday Avatar answered Oct 24 '22 00:10

DhiyanEswar Uday