我正在使用AngularFire处理一个简单的应用程序,并且使用AngularFire指南中的ui-router example保护了一些路由。
当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: []
拜托,你能帮我解决这个恼人的错误吗?
答案 0 :(得分:1)
添加
event.preventDefault()
停止初始状态
source 'https://rubygems.org'
ruby '2.3.0'
gem 'rails', '4.2.7'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'devise'
gem 'bootstrap-sass', '~> 3.3.6'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
group :production do
gem 'rails_12factor'
gem 'ibm_db'
end
group :development, :test do
gem 'byebug'
end
group :development do
gem 'web-console', '~> 2.0'
gem 'spring'
gem 'sqlite3'
end
答案 1 :(得分:0)
Auth.$requireSignIn
会抛出错误AUTH_REQUIRED
。您可以通过监控发现$stateChangeError
错误的AUTH_REQUIRED
来尝试重定向到正确的状态。
// 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");
}
});
}]);