本文介绍了如何使用 $transitions?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在使用:

  • "angular-ui-router": "^0.4.2"
  • 角度":^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!

当前实现:

'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);
  });

当前错误:

未捕获的错误:[$injector:unpr] 未知提供者:$transitionsProvider

推荐答案

新版本的 $transitions (>= 1.0.0)(PLUNKER 演示)

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){...});

太详细看每个事件方法:$transition 服务文档
还有一些示例:0.4 的迁移示例.2 到 1.0.0 在官方文档中

$state 更改旧版本 ( (PLUNKER 演示):

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){...});
}

检查 angular-ui-router 文档 获取更多 $state 更改事件

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

这篇关于如何使用 $transitions?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 18:45