我有一个名为users
的对象,用于存储每个name
用户的个人资料信息(email
和uid
)。
我试图弄清楚当用户登录并在DOM中显示时检索此配置文件信息的方法。这样做的好习惯是什么?
这是我到目前为止所尝试的内容。虽然currentAuth
已正确解析(因此authData.uid
),但它不会在$scope.User
中加载配置文件信息。
国
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state("home", {
url: "/home",
templateUrl: "templates/home.html",
controller: "AppCtrl",
resolve: {
// controller will not be loaded until $waitForAuth resolves
// Auth refers to our $firebaseAuth wrapper in the example above
"currentAuth": ["Auth", function(Auth) {
// $waitForAuth returns a promise so the resolve waits for it to complete
return Auth.$waitForAuth();
}]
}
})
.state('app', {
url: "/app",
abstract: true,
templateUrl: "templates/menu.html",
controller: 'AppCtrl',
resolve: {
// controller will not be loaded until $requireAuth resolves
// Auth refers to our $firebaseAuth wrapper in the example above
"currentAuth": ["Auth", function(Auth) {
// $requireAuth returns a promise so the resolve waits for it to complete
// If the promise is rejected, it will throw a $stateChangeError (see above)
return Auth.$requireAuth();
}]
}
})
.state('app.account', {
url: "/account",
views: {
'menuContent': {
templateUrl: "templates/account.html",
controller: 'AccountCtrl'
}
}
})
// other states
$urlRouterProvider.otherwise('/app/playlists');
});
帐户Ctrl
.controller('AccountCtrl', function($scope, Users, currentAuth) {
if(currentAuth) {
$scope.User = Users.getUser(currentAuth.uid);
$scope.authData = currentAuth;
}
})
用户服务
.factory('Users', function() {
var ref = new Firebase("https://<yourapp>.firebaseio.com/");
return {
getUser: function(userId){
var refChild = ref.child("users").child(userId);
var fnReturn = function(snap) {
console.log(snap.val()) // works perfect!
return snap.val();
}
return refChild.on('value', fnReturn)
}
}
})