0

我发现了一些奇怪的东西。请帮忙!

$scope.login = function() {
    ref.authWithPassword({
        email: $scope.user.email,
        password: $scope.user.password
    }, function(error, authData) {
        if (error) {
            console.log("Login Failed!", error);
            $scope.message = error.toString();
        } else {
            $location.path('/meetings');
            console.log("Authenticated successfully with payload:", authData);
        }
    });
} //login

这是一个登录功能,效果很好。但是,error提交登录后我得到了 3、4 秒。我注意到我{{message}}在收到价值后并没有立即更新$scope.message。我认为 Angular 应该在它发生变化时立即显示该值。?

第二次点击后,我得到了显示的错误。

这是我打印值的地方:

<p class="error formerror" ng-show="message">{{message}}</p>

4

1 回答 1

1

您正在调用authWithPassword,它是 Firebase 常规 JavaScript SDK 的一部分。此 API 将启动身份验证过程并在身份验证完成时调用您的函数。不幸的是,此时 AngularJS 不再知道您对$scope.

要让 AngularJS 知道更新,请将您的代码包装在一个$timeout调用中:

$scope.login = function() {
    ref.authWithPassword({
        email: $scope.user.email,
        password: $scope.user.password
    }, function(error, authData) {
        $timeout(function() {
            if (error) {
                console.log("Login Failed!", error);
                $scope.message = error.toString();
            } else {
                $location.path('/meetings');
                console.log("Authenticated successfully with payload:", authData);
            }
        });
    });
} //login

请注意,正是出于这个原因,AngularFire 在其$firebaseAuth服务中围绕这些身份验证功能提供了方便的包装器。请参阅AngularFire 指南中有关登录用户的部分

您正在寻找的包装函数是,在其 API 文档中阅读如何使用$authWithPassword的示例。$authWithPassword

于 2015-07-05T16:42:10.000 回答