AngularJS - UI 路由器 stateChangeSuccess 事件未触发 [英] AngularJS - UI Router stateChangeSuccess event not firing
问题描述
我正在使用 UI 路由器我的角度应用程序.我正在尝试集成 state change
事件,但它们不会触发状态更改.其他一切正常,控制台没有错误.我遇到了以下类似的问题,但没有一个解决方案对我有用:
I am using UI Router in my angular app. I am trying to integrate state change
events, but they are not firing on state change. Everything else is working fine and there is no error in console. I came across following similar questions, but none of the solution worked for me:
angular + ui-router: $stateChangeSuccess 在状态 b 上触发但不在 ab 上触发
以下是我的 Angular 代码:
Following is my Angular code:
(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'
})
});
}());
毫无头绪地离开了.我不确定我错过了什么.
Left with no clue. I am not sure what I am missing.
推荐答案
StateChange 事件已弃用 ui.router >= 1.0
StateChange events has been deprecated for ui.router >= 1.0
对于新的 ui.router
使用以下内容
for the new ui.router
use the following
StateChangeSuccess
StateChangeSuccess
$transitions.onSuccess({}, function() {
console.log("statechange success");
});
状态改变开始
$transitions.onStart({}, function(trans) {
console.log("statechange start");
});
查看此迁移指南了解更多信息
这篇关于AngularJS - UI 路由器 stateChangeSuccess 事件未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!