myApp.controller('RegistrationController', ['$scope',
function($scope) {
var auth = firebase.database().ref();
// console.log("auth::"+auth);
$scope.login = function() {
$scope.message = "Welcome " + $scope.user.email;
}; //login
$scope.register = function() {
console.log($scope.user.email);
console.log($scope.user.password);
console.log("jdxnx::" + auth);
firebase.auth().createUserWithEmailAndPassword($scope.user.email, $scope.user.password).then(function() {
$scope.message = "Hi" + $scope.user.email + " you have registered"
}).catch(function(error) {
$scope.message = error.message;
console.log($scope.message);
}); // //createUser
}; // register
}
]); // Controller
在上面的代码中,当我单击“提交”时,运行我的注册函数,然后在其中运行
firebase.auth().createUserWithEmailAndPassword
,但是在我得到firebase.auth().createUserWithEmailAndPassword($scope.user.email,$scope.user.password)
函数的响应之前,我的事件就从它出来,然后从注册函数中退出,并由于值在html页面中保持空白,尽管在后端{{message}}
函数处理并获取响应,但事件在处理完成之前返回页面。我希望该功能事件应该等到我得到响应。请注意,我正在使用firebase 3.x.x。
最佳答案
firebase.auth().createUserWithEmailAndPassword
是一个异步函数,不属于Angular.js。因此,Angular.js无法识别其对$scope
的回调所做的修改,并且不会更新页面。回调完成后,尝试添加$scope.apply()
。
firebase.auth().createUserWithEmailAndPassword($scope.user.email, $scope.user.password).then(function() {
$scope.message = "Hi" + $scope.user.email + " you have registered"
$scope.apply();
}).catch(function(error) {
$scope.message = error.message;
console.log($scope.message);
$scope.apply();
});
您可能感兴趣的其他内容:
看一下AngularFire项目,它将处理Firebase操作后触发Angular.js的问题。
不建议您将Angular.js与独立控制器和
$scope
一起使用,并且已经有相当一段时间了。您可能想看看Controller-As语法(Angular.js 1.3+)或更好的组件(Angular.js 1.5+)。关于javascript - Firebase响应后未更新Angular.js页面,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40118734/