AngularFire 使用路由器进行身份验证 $digest 循环错误

AngularFire authenticating with routers $digest loop error

我正在使用 AngularFire 开发一个简单的应用程序,一些路由使用 ui-router example 来自 AngularFire guide.

进行保护

当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: []

拜托,你能帮我解决这个恼人的错误吗?

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");
    }
  });
}]);

添加

event.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");
 }