AngularJs 运行 块 $stateChangeStart 未被触发

AngularJs Run block $stateChangeStart not being fired

我的AngularJs 运行块在应用程序更改路由时不执行处理函数,至少我是这么认为的。我尝试放置一个简单的控制台日志进行测试,但它没有记录任何内容。最后的objective是一个路由拦截器,检查路由是否需要认证。

Link 用于 github 存储库 https://github.com/guilinden/authenticatedCRUD

(function(){
    angular
    .module('app')
    .run(function ($rootScope, $location) {
    $rootScope.$on('$stateChangeStart', function (event, toState, toParams) {
        var requireLogin = toState.data.requireLogin;

        if(requireLogin){
            event.preventDefault();
            $location.path('/home');
        }

      });
   });    
})();

您的控制台中应该有 app not defined 错误。您缺少空依赖项

 angular
    .module('app',[])

也将其更改为 $location 作为对 运行、

的依赖
  .run(function ($rootScope,$location) {
    $rootScope.$on('$stateChangeStart', function (event, toState, toParams) {

$stateChange* 事件已弃用

$stateChange* 事件在 1.0 中已弃用。它们被替换为 $transitions.on* 转换挂钩。

但是,可以通过包含 stateEvents.js 并根据 'ui.router.state.events' angular 模块重新启用它们以实现向后兼容性。

<script src="stateEvents.js">
var myApp = angular.module('myApp', ['ui.router', 'ui.router.state.events']);

UI-router Issue #2720 - $stateChangeStart not being fired on v1.0