我使用angular2 + firebase。 我的代码有问题。 当用户登录时,函数createUser和removeUser无法正常工作。没有登录它可以正常工作。我从firebase教程获得了部分代码。当我尝试使用谷歌身份验证服务时,同样的事情。
app.controller("AuthCtrl", ["$scope", "$firebaseAuth",
function($scope, $firebaseAuth) {
var ref = new Firebase("...");
var Auth = $firebaseAuth(ref)
$scope.createUser = function() {
$scope.message = null;
$scope.error = null;
Auth.$createUser({
email: $scope.email,
password: $scope.password
}).then(function(userData) {
$scope.message = "User created with uid: " + userData.uid;
$scope.email = "";
$scope.password = "";
}).catch(function(error) {
$scope.error = error;
});
};
$scope.removeUser = function() {
$scope.message = null;
$scope.error = null;
Auth.$removeUser({
email: $scope.email,
password: $scope.password
}).then(function() {
$scope.message = "User removed";
$scope.email = "";
$scope.password = "";
}).catch(function(error) {
$scope.error = error;
});
};
$scope.login = function(){
Auth.$authWithPassword({
email: $scope.emaillog,
password: $scope.passwordlog
}).then(function(authData) {
$scope.authData = Auth.$getAuth();
console.log("Logged in as:", authData.uid);
}).catch(function(error) {
console.error("Authentication failed:", error);
});
}
}
]);
<div ng-app="App">
<div ng-controller="AuthCtrl">
<div>
<h2>Login</h2>
<form>
Email: <input ng-model="emaillog" type="text" placeholder="email">
Password: <input ng-model="passwordlog" type="text" placeholder="password">
<br><br>
<button ng-click="login()">Login</button>
</form>
</div>
<div ng-if = "authData">
<h2>Managing users</h2>
<form>
Email: <input ng-model="email" type="text" placeholder="email">
Password: <input ng-model="password" type="text" placeholder="password">
<br><br>
<button ng-click="createUser()">Create User</button>
<button ng-click="removeUser()">Remove User</button>
<p ng-if="message">Message: <strong>{{ message }}</strong></p>
<p ng-if="error">Error: <strong>{{ error }}</strong></p>
</form>
</div>
</div>
</div>
我在这里找到了答案what is the difference between ng-if and ng-show/ng-hide