AngularJS & Firebase auth ngShow 和 ngClick 延迟?

AngularJS & Firebase auth ngShow and ngClick delay?

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

当我第一次点击登录按钮时,没有任何反应。当我第二次点击它时,登录状态发生变化并且 ng-show 和 ng-hide div 切换。

点击退出时出现同样的问题。

为什么只在第二次点击后出现?

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