角渲染引擎和放大器;火力地堡的个人资料检索 [英] Angular rendering engine & Firebase profile retrieval
问题描述
我要什么:
- 页面加载的火力点检查认证
- 火力如果登录用户ID返回
- 我的函数会返回与 user.Id 相关的用户名
- 分配给重新presents的用户名 变量
- 渲染!
- 所有在页面加载
当前的行为:
下面的配置获取用户名,但一旦我点击登录按钮我即使我当前登录made.For某种原因,我必须点击登录按钮只显示用户名。我希望有一个设置在那里,如果我登录的应用程序将只知道我从一开始就登录了!
crossfitApp.controller('globalIdCtrl',[$范围,$ Q','defautProfileData','$超时',函数($范围,$ Q,defautProfileData, $超时){ VAR dataRef =新的火力地堡(https://glowing-fire-5401.firebaseIO.com); $ scope.myFbvar = NULL;
$ scope.authenticated = {
的currentUser:空,
avatarUrl:,
电子邮件地址: ,
设置:,
currentUserid:空, };
功能getProfile(用户ID,assignMe){ 我的配置文件=新的火力地堡(https://glowing-fire-5401.firebaseio.com/profiles/+用户ID +/用户名);
myprofile.once('值',函数(nameSnapshot){
assignMe = nameSnapshot.val();
});
}; $ scope.auth =新FirebaseSimpleLogin(dataRef,功能(错误,用户){ 如果(错误){
//错误
的console.log('错误'); }
否则如果(用户){
//登录
$超时(函数(){ getProfile(user.id,); });
的console.log('登录');
$ scope.authenticated.currentUserid = user.id;
}
其他{
//用户被注销
的console.log('退出');
$超时(函数(){
$ scope.authenticated.currentUserid = NULL;
});
}
});
}]); //全球
在你的否则,如果(用户)
逻辑,你忘了把你的范围变种的$超时内,所以它被设置正确,但角度并不了解它,直到下一次$申请被调用(如NG单击,NG-提交等)。
因此:
否则,如果(用户){
//登录
$超时(函数(){
getProfile(user.id,);
$ scope.authenticated.currentUserid = user.id; //搬进$超时
});
的console.log('登录');
}
您可以阅读更多关于为什么这重要的<一个href=\"http://stackoverflow.com/questions/22119461/firebase-authentication-on-app-initilisation\">here和这里。
What I want:
- firebase checks authentication of page load
- firebase returns userID if logged in
- my function returns the username associated with the user.Id
- assign to the variable that represents the username
- render!
- All on page load
Current Behavior:
The following configuration retrieves the username but will only display the username once I click a login button I have made.For some reason even though I am currently logged in I must click the login button. I want a set up where if I am logged in the app will just know I am logged in from the start!
crossfitApp.controller('globalIdCtrl', ["$scope",'$q','defautProfileData','$timeout', function ($scope,$q,defautProfileData,$timeout) {
var dataRef = new Firebase("https://glowing-fire-5401.firebaseIO.com");
$scope.myFbvar =null;
$scope.authenticated={
currentUser: null,
avatarUrl: "",
emailAddress: "",
settings: "",
currentUserid: null,
};
function getProfile(userID,assignMe){
myprofile= new Firebase("https://glowing-fire-5401.firebaseio.com/profiles/"+userID+"/username");
myprofile.once('value', function(nameSnapshot) {
assignMe = nameSnapshot.val();
});
};
$scope.auth = new FirebaseSimpleLogin(dataRef, function(error, user) {
if (error) {
//Error
console.log ('error');
}
else if (user) {
//logged in
$timeout(function() {
getProfile(user.id,);
});
console.log('logged in');
$scope.authenticated.currentUserid = user.id ;
}
else {
// user is logged out
console.log('logged out');
$timeout(function() {
$scope.authenticated.currentUserid =null;
});
}
});
}]); //Global
In your else if( user )
logic, you forgot to put your scope var inside the $timeout, so it is being set properly, but Angular doesn't learn about it until the next time $apply is called (e.g. ng-click, ng-submit, etc).
Thus:
else if (user) {
//logged in
$timeout(function() {
getProfile(user.id,);
$scope.authenticated.currentUserid = user.id ; // moved into $timeout
});
console.log('logged in');
}
You can read more about why this matters here and here.
这篇关于角渲染引擎和放大器;火力地堡的个人资料检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!