【发布时间】:2017-03-12 02:18:21
【问题描述】:
我正在尝试从用户的数据库中获取用户信息并使用 rootscope 从任何地方访问它。
我在用户登录时获取用户的电子邮件和 uid。
但是,要从数据库中获取用户的信息,我需要调用我的数据库并一一读取信息并将其作为变量存储在 rootscope 中以便从任何地方访问它。
所以,我的问题如下:
- 如何在本例中使用 rootscope?
- 有没有更好的方法来做到这一点?
- 在下面的示例中,控制台日志显示了名字,但我不知道如何对其进行根范围检查?
感谢您的帮助。
app.run(['$rootScope', '$state', '$stateParams', '$cookies', "$location",
function ($rootScope, $state, $stateParams, $cookies, $location) {
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
$rootScope.user.uid = user.uid;
$rootScope.user.email = user.email;
return firebase.database().ref('/users/' + user.uid).once('value').then(function(snapshot) {
var firstname = snapshot.val().firstname;
console.log("first name", firstname);
});
} else {
// No user is signed in.
}
});
}]);
【问题讨论】:
标签: angularjs firebase firebase-realtime-database firebase-authentication