AngularJS - UI Router状态变化成功事件未触发

26

我在我的angular应用程序中使用UI Router。 我正在尝试集成状态更改事件,但它们在状态更改时没有触发。 其他所有功能都正常工作,并且控制台中没有错误。 我遇到了以下类似的问题,但没有任何解决方案适用于我:

$rootScope.$on("$routeChangeSuccess) 或 $rootScope.$on("$stateChangeSuccess) 在使用ui-router(AngularJS)时不起作用

angular + ui-router:$stateChangeSuccess在状态b上触发,但不在a.b上触发

以下是我的Angular代码:

(function() {

    angular.module("bootdemo", [
        "ngResource",       
        "ui.router",
        "bootdemo.core",
        "bootdemo.index"        
    ])
    .run(function ($rootScope, $location, $state, $stateParams) {

        $rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams){ 
            alert("root change success");
        })

        $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams, options){ 
            alert("root change start");
        })

        $rootScope.$on('$stateChangeError', function(event, toState, toParams, fromState, fromParams, error){ 
            alert("root change error");
        })
    })
    .config(function($stateProvider, $urlRouterProvider){
        $urlRouterProvider.otherwise('/');
        $stateProvider
            .state('index', {
                url: "/",
                templateUrl: '/index/templates/welcome.html',
                controller: 'IndexController as vm' 
            })
            .state('login', {
                url: "/login",
                templateUrl: '/index/templates/login.html',
                controller: 'LoginController as ctrl'   
            })
            .state('home', {
                url: "/home",
                templateUrl: '/index/templates/home.html',
                controller: 'HomeController as ctrl'    
        })
    });



}());

束手无策。我不确定我错过了什么。


我曾经遇到过“alpha”版本的问题,如果您正在使用这个版本,请降级。 - ThomasP1988
干得好!我也遇到了同样的问题。谢谢@ThomasP1988 - hemu
3个回答

38

StateChange事件在ui.router >= 1.0版本已被弃用。

对于新的ui.router版本,请使用以下内容:

StateChangeSuccess。

$transitions.onSuccess({}, function() {
  console.log("statechange success");
});

状态变更开始

$transitions.onStart({}, function(trans) {
 console.log("statechange start");
});

查看此迁移指南以获取更多信息。


14

1

$state事件已经在Angular版本>1.0.0中弃用。 现在我们必须使用$transitions来进行更改事件。

请从这里参考$transitions


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接