AngularJS - UI路由器stateChangeSuccess事件未触发

hem*_*emu 22 angularjs single-page-application angular-ui-router

我在我的角应用程序中使用UI路由器.我正在尝试整合state change事件,但他们没有解决状态变化问题.其他一切工作正常,控制台没有错误.我遇到了类似的问题,但没有一个解决方案对我有用:

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

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

以下是我的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'    
        })
    });



}());
Run Code Online (Sandbox Code Playgroud)

没有任何线索.我不确定我错过了什么.

Sib*_*raj 32

StateChange事件已被弃用 ui.router >= 1.0

对于新的ui.router使用以下

StateChangeSuccess

$transitions.onSuccess({}, function() {
  console.log("statechange success");
});
Run Code Online (Sandbox Code Playgroud)

StateChangeStart

$transitions.onStart({}, function(trans) {
 console.log("statechange start");
});
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请查看此迁移指南


Pra*_*eek 14

如果您使用新的ui-router(v1.0.0),则$stateChange*事件将不起作用.$transitions.on*从现在开始你必须使用钩子.

你可以在这里阅读.

https://ui-router.github.io/docs/latest/modules/ng1_state_events.html

https://github.com/angular-ui/ui-router/issues/2720