AngularJS,$digest 错误尝试在需要登录的页面上重定向应用程序 [英] AngularJS, $digest error trying to redirect application on pages requiring login

查看:33
本文介绍了AngularJS,$digest 错误尝试在需要登录的页面上重定向应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在请求的状态需要登录的情况下重定向 AngularJS 应用程序.这是我在 angular 应用程序的运行"方法中放置的方法:

I'm trying to redirect an AngularJS app in the case that the requested state requires login. This is the method I've placed within the 'run' method on my angular app:

$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {

  if(toState.data.requiresLogin && !User.isLoggedIn)
  {
    event.preventDefault();
    $state.go('login');
  }
});

看起来很简单,但我收到了这个摘要错误.它似乎是由 event.preventDefault() 语句触发的.我当然不能删除它...

Seems straightforward, but I'm getting this digest error. It appears to be triggered by the event.preventDefault() statement. I can't remove that of course...

Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: [["fn: $locationWatch; newVal: 7; oldVal: 6"],["fn:     $locationWatch; newVal: 8; oldVal: 7"],["fn: $locationWatch; newVal: 9; oldVal: 8"],["fn: $locationWatch; newVal: 10; oldVal: 9"],["fn: $locationWatch; newVal: 11; oldVal: 10"]]
...

关于如何重构以清除此错误的任何建议,或者错误甚至意味着什么?

Any advice on how to refactor this to clear this error, or what the error even means?

推荐答案

有一个工作示例,如何重定向到登录.首先定义了 4 个状态 - 其中 2 个需要 身份验证,第三个是 public (对于任何人),然后也是登录状态:

There is a working example, how to redirect to login. Firstly there are 4 states defined - 2 of them require authentication, third is public (for anybody) and there is also login state:

$stateProvider
    // available for anybody
    .state('public',{
        url : '/public',
        template : '<div>public</div>',
    })
    // just for authenticated
    .state('some',{
        url : '/some',
        template : '<div>some</div>',
        data : {requiresLogin : true },
    })
    // just for authenticated
    .state('other',{
        url : '/other',
        template : '<div>other</div>',
        data : {requiresLogin : true },
    })
    // the log-on screen
    .state('login',{
        url : '/login',
        templateUrl : 'tpl.login.html',
        controller : 'UserCtrl',
    })

这将是 $stateChangeStart def,它总是重定向到 login - 如果需要......只有 login itslef 和 public 始终可用/对任何人

and that would be the $stateChangeStart def, which always redirects to login - if required ... only login itslef and public are available always/to anybody

.run(['$rootScope', '$state', 'User', function($rootScope, $state, User)
{

  $rootScope.$on('$stateChangeStart'
    , function(event, toState, toParams, fromState, fromParams) {

    var isAuthenticationRequired =  toState.data 
          && toState.data.requiresLogin 
          && !User.isLoggedIn
      ;

    if(isAuthenticationRequired)
    {
      event.preventDefault();
      $state.go('login');
    }
  });
}])

检查所有操作此处

这篇关于AngularJS,$digest 错误尝试在需要登录的页面上重定向应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆