【发布时间】:2015-02-01 05:05:59
【问题描述】:
刚开始学习 Angular,但我一直在用 jQuery 制作网站,现在开始开发一些与网站连接到相同 firebase 的应用程序。
我的问题是我找不到获取子属性并在 ref 中使用它的方法。我想在下面的示例中获取值“示例”:
users: {
simplelogin:1 {
CurrentGroup : "example"
}
}
我通常用 jQuery 做的事情是这样的:
currentUserRef.on('value', function(snapshot){
currentGroup = snapshot.val().CurrentGroup;
});
var checkCurrentGroup = setInterval (function(){
if ( currentGroup !== undefined ) {
clearInterval(checkCurrentGroup);
var currentGroupUsersRef = new Firebase(FB + "/groupUsers/" + currentGroup);
//Rest of the code for the page/app
}}, 200);
那么,这在 Angular 中是如何完成的呢?我的尝试是复制 firebase 指南上的示例:
app.factory("Profile", ["$firebase", function($firebase) {
return function(username) {
var userRef = new Firebase(FB + "/users/" + username);
return $firebase(userRef).$asObject();
}
}]);
app.controller("ProfileCtrl", ["$scope", "Profile",
function($scope, Profile) {
console.log("profile = ", Profile(uid));
console.log("profile current group = ", Profile(uid).CurrentGroup);
}
]);
控制台日志:
profile = d {$$conf: Object, $id: "simplelogin:76", $priority: null, $save: function, $remove: function...}$$conf: Object$id: "simplelogin:76 "$priority: nullCurrentGroup: "Big Group"displayName: "Jim"email: "someone@email.com"firstLogin: trueprovider: "password"provider_id: "simplelogin:76"proto: Object
配置当前组 = 未定义
我可以选择用户对象,但是当我选择user.currentGroup 时,它将是未定义的。不知道我是否在正确的轨道上。
【问题讨论】:
-
"当我执行 user.currentGroup 时,它会是未定义的"您能否编辑您的问题,使其还包含执行此操作的代码(从而触发错误)?
-
对不起,我有点傻没有把它放进去。现在添加它。
-
我会在下面写一个快速的答案,但同时已经阅读:stackoverflow.com/questions/27049342/…
标签: angularjs firebase angularfire