【发布时间】:2014-12-02 16:29:54
【问题描述】:
大家好,我正在使用 firebase 和 Angular 来构建示例应用程序。
这是注册函数
$scope.register = function (){
Authentication.register($scope.user)
.then(function(user){
Authentication.login($scope.user);
})
.then(function(user){
$timeout(function(){
$location.path('/meetings');
}, 5);
})
.catch(function(error){
$scope.message = error.toString();
});
} //register
此函数调用此工厂中的方法
myApp.factory('Authentication', function($firebase,
$firebaseAuth,$location, FIREBASE_URL){
var ref = new Firebase (FIREBASE_URL);
var auth = $firebaseAuth(ref);
var myObject = {
login : function (user){
return auth.$authWithPassword({
email: user.email,
password: user.password
});
}, // login
logout: function (){
return auth.$unauth();
},
register : function (user){
return auth.$createUser(user.email,user.password);
} //register
} //myObject
return myObject;
});
这里是 status.js 控制器,它根据用户是否登录来更改我的索引
auth.$onAuth(function(authData){
if (authData){
console.log("i entered authData");
$scope.userStatus = authData.password.email;
} else {
$scope.userStatus = false;
}
});
index.html 文件的一部分
<div class="userinfo"
ng-controller="StatusController" ng-show="userStatus">
<span class="user">Hi {{userStatus}}</span>
<a href="#" ng-click="logout()">Log Out</a>
</div>
我的问题是 ng-view 需要刷新页面才能显示新值。它没有自动显示,但我的代码有效。如果我手动刷新页面,我可以看到用户已注册并登录。
现在搜索大约 2 小时,$scope.$apply here 似乎不是这样。
提前谢谢你
【问题讨论】:
-
不知道答案,但是文档中提到的是
onAuth而不是$onAuth,会不会是这样? -
尽量让 ng-show 不与控制器内联。
-
为什么奇怪的
myObject包装器什么都不做?考虑只使用return $firebaseAuth(ref);
标签: angularjs firebase registration ng-show