How to use $transitions?

2019-03-13 12:59发布

Currently, I'm using:

  • "angular-ui-router": "^0.4.2"
  • "angular": "^1.6.3"
  • "webpack": "^2.4.1"

I am aware of my current implementation might be deprecated, just looking for the implementation(an example or documentation) of the new method. Any help is greatly appreciated, thanks in advance!

Current implementation:

'use strict';

module.exports = angular
  .module('common', [
    'ui.router',
    'angular-loading-bar',
    require('./header').name,
    require('./sideBar').name,
    require('./footer').name
  ])
  .run(function($transitions, cfpLoadingBar) {
    $transitions.onStart({}, cfpLoadingBar.start);
    $transitions.onSuccess({}, cfpLoadingBar.complete);
  });

Current error:

Uncaught Error: [$injector:unpr] Unknown provider: $transitionsProvider <- $transitions

1条回答
你好瞎i
2楼-- · 2019-03-13 13:16

In new versions (>=1.0.0) the $state change events are deprecated, and now you have to use the $transitions instead...

$transitions for new versions (>= 1.0.0) (PLUNKER DEMO)

MyCtrl.$inject = ['$transitions'];

function MyCtrl($transitions) {
    $transitions.onSuccess({}, function($transition){
        console.log($transition.$from());
        console.log($transition.$to());
        console.log($transition.params());
    });
}

Available events ordered by invocation:

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

$transitions.onExit({exiting: "stateName"}, function($transition){...});

$transitions.onRetain({}, function($transition){...});

$transitions.onEnter({entering: "stateName"}, function($transition){...});

$transitions.onFinish({}, function($transition){...});

$transitions.onSuccess({}, function($transition){...});

Too see each event method in detail: $transition service docs
Also some examples: Migrations examples from 0.4.2 to 1.0.0 in official docs


$state changes events for old versions (<= 0.4.2) (PLUNKER DEMO):

MyCtrl.$inject = ['$scope'];

function MyCtrl($scope) {
    $scope.$on('$stateChangeStart', 
        function(event, toState, toParams, fromState, fromParams, options) {...});

    $scope.$on('$stateChangeSuccess', 
        function(event, toState, toParams, fromState, fromParams, options){...});
}

Check angular-ui-router docs for more $state change events

查看更多
登录 后发表回答