javascript - AngularFire(Angular + Firebase)身份验证错误延迟

标签 javascript angularjs firebase angularfire firebase-authentication

我发现了一些奇怪的事情。请帮忙!

$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>

最佳答案

您正在调用 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 服务中提供了围绕这些身份验证功能的便捷包装器。请参阅section in the AngularFire guide on logging users in

您正在寻找的包装函数是$authWithPassword,请阅读 how to use $authWithPassword in its API documentation 的示例.

关于javascript - AngularFire(Angular + Firebase)身份验证错误延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31232136/

相关文章:

javascript - 为什么我的图表在 Chrome 中是错误的?

javascript - 如何为 Bootstrap 选项卡 Pane 提供向上滑动/向下滑动效果

javascript - 加载 Angular View 时动画 div block

google-app-engine - 从 Firebase 函数调用 AppEngine

ios - recaptcha 验证后的空白页使用电话号码 Swift 在 iOS 上使用 Firebase 进行身份验证

javascript - Zend_Dojo_Form 元素无法正常工作

javascript - 配置 webpack encore 进行异步和等待(Symfony 4 和 VueJs)

javascript - 参数 '' 不是函数,在 Kendo ( AngularJS ) 中未定义

javascript - 尽管在 $watch 函数中设置了 $scope 变量,但 $scope 变量未定义

swift - 处理来自 firebase 的 Null ref