AngularJS & Firebase auth ngShow and ngClick delay?
AngularJS & Firebase auth ngShow and ngClick delay?
我刚开始使用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;
});
相关文章:
- Selenium WebDriver and JavaScript change
- PHP and Javascript functions
- Javascript Return and if/else
- SetTimeout and clearTimeout in Javascript
- lightbox in html 5 and javascript
- TimelineJS and AngularJS
- timeago.js with datatable and PHP
- javascript button ajax and php
- Ajax and Json with Rails
- Combine onload, onresize and onclick
- php布尔值's小写AND大写和数字布尔值'可以接受
- setTimeout and V8
- Jquery post and onclick
- Moment js and IOS
- Rails 3.2 and Paloma gem
- Backbone, TypeScript, and super
- iframe and JavaScript on different pages
- JSON and jQuery Search
- Angular and SEO, ngClick vs href
- AngularJS & Firebase auth ngShow and ngClick delay?