Ui-router 1.0.0.beta1 $ transitions.onSuccess from $ rootScope.on ('$ stateSuccess',

In my AngularJS (v. 1.5.x), I convert my controller-based states to composite states using ui-router 1.0. 0 (currently in beta 1) .

I'm having trouble capturing state changes that were previously caught using $rootScope.on('$stateSuccess', .... As far as I can tell, I am doing what is described in the documents , and this is the SO question , so I wonder if I am missing something or if this is a bug in the beta release.

I have not converted all controllers, only a few.

The converted ones look like this (based on information obtained from the RFC: Routing to Angular 1.5.component () ):

.state('componentstate', {
    url: '/myUrl',
    component : 'myComponent'
});

Navigating to these states works fine.

To update my code to the new logic Transitions, I change this:

$rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams){
    if($state.current.name == 'acontrollerbasedstate') // do stuff;
});

:

$transitions.onSuccess( { to: 'acontrollerbasedstate', from: '*' }, function() {
    // I've also tried { to: '*', from: '*' }
    console.log('running');
    // do stuff
});

Since it did not work, I also tested all the methods:

$transitions.onBefore( { to: '*', from: '*' }, function() {
  console.log('onBefore');
});
$transitions.onEnter( { to: '*', from: '*' }, function() {
  console.log('onEnter');
});
$transitions.onError( { to: '*', from: '*' }, function() {
  console.log('onError');
});
$transitions.onExit( { to: '*', from: '*' }, function() {
  console.log('onExit');
});
$transitions.onFinish( { to: '*', from: '*' }, function() {
  console.log('onFinish');
});
$transitions.onRetain( { to: '*', from: '*' }, function() {
  console.log('onRetain');
});
$transitions.onStart( { to: '*', from: '*' }, function() {
  console.log('onStart');
});
$transitions.onSuccess( { to: '*', from: '*' }, function() {
  console.log('onSuccess');
});

Unfortunately, nothing is printed in the console, which means that the callback function $transitions.onSuccesswill never be executed.

//////
UPDATE
//////

I tried without an ad toor from, and it works:

$ transitions.onStart ({}, scrollToTop);

$transitions.onSuccess({}, function(){
    // here I use $scope.current.name, which for my purposes, works fine for now
});
+4
source share

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


All Articles