How to use $transitions?

All we need is an easy explanation of the problem, so here it is.

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

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

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

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply