当使用$ state.go时,在angularjs中达到了10个摘要迭代

时间:2015-08-19 11:57:23

标签: angularjs angular-ui-router

我使用角度与离子。

我不希望用户必须再次登录,以防他已经登录并且上次使用该应用程序时没有注销。因此,我使用本地存储来检查用户是否已登录。 我尝试将此代码放在运行块中。

$rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
    if($localStorage.userInfo){
        $state.go('Deployment');   //This is line 270 of app.js
         event.preventDefault();
                return;

        }
      })

但它会出错:

RangeError: Maximum call stack size exceeded
    at Scope.$broadcast (ionic.bundle.js:23414)
    at Object.transitionTo (ionic.bundle.js:40804)
    at Object.go (ionic.bundle.js:40671)
    at app.js:270
    at Scope.$broadcast (ionic.bundle.js:23412)
    at Object.transitionTo (ionic.bundle.js:40804)
    at Object.go (ionic.bundle.js:40671)
    at app.js:270
    at Scope.$broadcast (ionic.bundle.js:23412)
    at Object.transitionTo (ionic.bundle.js:40804)

此外,

Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: []
http://errors.angularjs.org/1.3.13/$rootScope/infdig?p0=10&p1=%5B%5D
    at ionic.bundle.js:8755
    at Scope.$digest (ionic.bundle.js:22973)
    at Scope.$apply (ionic.bundle.js:23198)
    at done (ionic.bundle.js:18351)
    at completeRequest (ionic.bundle.js:18541)
    at XMLHttpRequest.requestLoaded (ionic.bundle.js:18482)

如何修复这些错误?

我的应用程序中的

配置如下所示:

.config(function($stateProvider,$urlRouterProvider){
  $stateProvider
  .state('Login',{
    url:'/login',
    templateUrl:'templates/login.html',
    controller:'LoginCtrl'
  })

  .state('Deployment',{
    url:'/deployment',
    templateUrl:'templates/deployment.html'
  })
  .state('Bill',{
    url:'/bills',
    templateUrl:'templates/bills.html'
  })
$urlRouterProvider.otherwise('/login');
})

编辑1: 阅读this github link

我将if条件改为

 if($localStorage.userInfo != null && toState.name != 'Deployment'){
            $state.go('Deployment');
             event.preventDefault();
                    return;

            }

Maximum call stack size exceeded went away

但我仍然留下10 digest iterations reached

修改:2

我改变了

`$urlRouterProvider.otherwise('/login');` in app.js

$urlRouterProvider.otherwise(function ($injector) {
           var $state = $injector.get('$state');
            $state.go('Login');
        }); 

之后我很确定我错过了if语句中的某些条件,即

   if($localStorage.userInfo != null && toState.name != 'Deployment')

我能否得到一些帮助,因为我可能错过了导致无限循环的内容?

1 个答案:

答案 0 :(得分:0)

查看ui-router http://angular-ui.github.io/ui-router/site/#/api/ui.router.state。$状态的文档,可以使用$stateChangeStart取消event.preventDefault()事件,但是在取消旧版本之前启动转换。

因此,由于toState.name不是一个记录的属性,它似乎会在条件下一次又一次失败并继续产生新的状态变化。

尝试:

if($localStorage.userInfo != null && toState.$current.name != 'Deployment'){
    event.preventDefault();
    $state.go('Deployment');
    return;
}