AngularJS & Firebase auth ngShow and ngClick delay?

AngularJS & Firebase auth ngShow and ngClick delay?

本文关键字:and ngClick delay ngShow auth amp Firebase AngularJS      更新时间:2023-09-26

我刚开始使用AngularJS,我遇到了一个关于Firebase身份验证的奇怪问题。我有一个非常简单的身体,显示当前的用户状态(登录,真或假)和一个signIn和signOut选项。

当我第一次点击登录按钮时,什么都没有发生。当我第二次点击它时,登录状态改变,ng-show和ng-hidediv切换。

点击登出也是同样的问题

为什么它只发生在第二次点击之后?

index . html:

<div class="container" ng-controller="userCtrl">    
    <h1>User logged in: {{loggedIn}}</h1>
    <div ng-hide="loggedIn">
        <div class="form-group">
            <label for="email">Email:</label> 
            <input type="email" class="form-control" name="email" ng-model="user.email" />
        </div>
        <div class="form-group">
            <label for="password">Password:</label> 
            <input type="password" class="form-control" name="password" ng-model="user.password" />
        </div>
        <button type="button" class="btn btn-lg btn-success" ng-click="signIn()">Sign in</button>
    </div>
    <div ng-show="loggedIn"><button type="button" class="btn btn-lg btn-danger" ng-click="signOut()">Sign out</button></div>
</div>

控制器:

var myApp = angular.module("tijdlozespelApp", ["firebase"]);
    myApp.controller("userCtrl", function ($scope, $firebaseObject) {   
    $scope.loggedIn = false;
    $scope.signIn = function() {
            var email = $scope.user.email;
            var password = $scope.user.password;
            firebase.auth().signInWithEmailAndPassword(email, password).then(function(user) {
            $scope.loggedIn = true;
        }).catch(function(error) {
            $scope.loggedIn = false;
        });
    }
    $scope.signOut = function() {
        firebase.auth().signOut().then(function() {
            $scope.loggedIn = false;
        });
    }   
});

使用$scope.$apply(function(){…})当你使用异步处理程序时更新你的作用域数据。Angular在识别异步操作的作用域变化方面存在问题。

$scope.$apply(function() {
   $scope.loggedIn = true;
});