我正在使用AngularFire开发一个简单的应用程序,其中一些路由使用AngularFire指南中的ui-router示例进行保护。
当URL为空时,ui-router用$urlRouterProvider.when('/', '/account');
处理它。但是当重定向URL被保护为:
.state('account', {
controller: 'AccountCtrl',
controllerAs: 'vm',
url: '/account',
templateUrl: 'app/account/account.html',
data: {
title: '- Account'
},
resolve: {
'currentAuth': ['Auth', function(Auth) {
return Auth.$requireSignIn();
}]
}
})
抛出以下错误:
Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: []
请问,你能帮我解决这个烦人的错误吗? 添加
事件。preventDefault ()
停止初始状态
if (error === "AUTH_REQUIRED") {
//halt default event then initiate state go to new nested page
event.preventDefault();
// as of now, we go to login until landing template is up
$state.go("home");
}
Auth.$requireSignIn
将抛出错误AUTH_REQUIRED
。您可以通过监视捕获AUTH_REQUIRED
错误的$stateChangeError
来尝试重定向到正确的状态。
// for ui-router
app.run(["$rootScope", "$state", function($rootScope, $state) {
$rootScope.$on("$stateChangeError", function(event, toState, toParams, fromState, fromParams, error) {
// We can catch the error thrown when the $requireSignIn promise is rejected
// and redirect the user back to the home page
if (error === "AUTH_REQUIRED") {
$state.go("home");
}
});
}]);