使用angularjs和angularfire在我的控制器中循环一个数组:
angular.forEach($scope.precourse, function(value, key){
console.log(value);
});
结果:
Object {$id: "-JUxrRDDip9wCwEx1t6j", $bind: function, $add: function, $save: function, $set: function…}
Object {$id: "-JUxrRDHo6Ph5_5_yUNF", $bind: function, $add: function, $save: function, $set: function…}
Object {$id: "-JUxrRDIGdTuhZkQQ73a", $bind: function, $add: function, $save: function, $set: function…}
打开一个对象我可以看到不同的属性:
Object {$id: "-JUxrRDIGdTuhZkQQ73a", $bind: function, $add: function, $save: function,
$set: function…}
$$hashKey: "00P"
$add: function (item) {
$auth: function (token) {
$bind: function (scope, name, defaultFn) {
$child: function (key) {
$getIndex: function () {
$getRef: function () {
$id: "-JUxrRDIGdTuhZkQQ73a"
$off: function (type, callback) {
$on: function (type, callback) {
$remove: function (key) {
$save: function (key) {
$set: function (newValue) {
$transaction: function (updateFn, applyLocally) {
$update: function (newValue) {
pairs: Object //The Objects I'm intrested in
responsible: Object //The Objects I'm intrested in
__proto__: Object
所以让我们打印出来!
console.log(value.pairs);
console.log(value.responsible);
结果
undefined schedule.js:38
-JUrw5QLvIaBaICuKdmC schedule.js:39
undefined schedule.js:38
-JUsWS58wX4Y_zEf7FqG schedule.js:39
undefined schedule.js:38
-JU_6xy9_JDkgrE1mPSb
如果我使用$ child()属性
console.log(value.$child('pairs'));
console.log(value.$child('responsible'));
Object {$id: "pairs", $bind: function, $add: function, $save: function, $set: function…}
Object {$id: "responsible", $bind: function, $add: function, $save: function, $set: function…}
Object {$id: "pairs", $bind: function, $add: function, $save: function, $set: function…}
Object {$id: "responsible", $bind: function, $add: function, $save: function, $set: function…}
Object {$id: "pairs", $bind: function, $add: function, $save: function, $set: function…}
Object {$id: "responsible", $bind: function, $add: function, $save: function, $set: function…}
这是为什么?我只是在寻找对象的实际值,我该如何获取它们?我想使用value.pairs来检索对,但这不会起作用。
修改 添加$ on('已加载',function())
代码现在看起来:
angular.forEach($scope.precourses, function(value, key){
value.$on('loaded', function(loadedValue) {
console.log(loadedValue);
console.log(loadedValue.pair);
});
});
提供输出:
Object {responsible: "-JU_6xy9_JDkgrE1mPSb"} schedule.js:40
undefined schedule.js:41
Object {responsible: "-JUrw5QLvIaBaICuKdmC"} schedule.js:40
undefined schedule.js:41
Object {responsible: "-JUsYm9UtONz0NzNTqbT"} schedule.js:40
undefined schedule.js:41
按按钮再次重复进行每次循环:
(after short delay)
Object {pairs: Object, responsible: "-JU_6xy9_JDkgrE1mPSb"} schedule.js:40
Object {-JUYhpPWOXqSpDNph5lo: "-JUYhpPWOXqSpDNph5lo", -JU_6xy9_JDkgrE1mPSb: "-JU_6xy9_JDkgrE1mPSb", -JUrw5QLvIaBaICuKdmC: "-JUrw5QLvIaBaICuKdmC"} schedule.js:41
Object {pairs: Object, responsible: "-JUsWS58wX4Y_zEf7FqG"} schedule.js:40
Object {-JUsWEf-v5XxIcLJSFgz: "-JUsWEf-v5XxIcLJSFgz", -JUsWS58wX4Y_zEf7FqG: "-JUsWS58wX4Y_zEf7FqG", -JUsYm9UtONz0NzNTqbT: "-JUsYm9UtONz0NzNTqbT"} schedule.js:41
Object {pairs: Object, responsible: "-JUrvf-rxHZPc4kF-IBb"} schedule.js:40
Object {-JUZSc-QEO0U0rf7hLV4: "-JUZSc-QEO0U0rf7hLV4", -JUdf8Z-uxlc0_sg-ZzB: "-JUdf8Z-uxlc0_sg-ZzB", -JUrvf-rxHZPc4kF-IBb: "-JUrvf-rxHZPc4kF-IBb"} schedule.js:41
编辑#2 好吧我想我发现了问题。它位于创建计划的服务文件中:
在/service/schedule.js里面
angular.forEach(preCourse, function (value, key){
scheds.$child(schedId).$child('courses').$child('precourses').$add({
responsible: value
}).then(function (ref) {
console.log('Inside Precourse');
scheds.$child(schedId).$child('courses').$child('precourses').$child(ref.name()).$child('pairs').$child(mainCourse[key]).$set(mainCourse[key]);
scheds.$child(schedId).$child('courses').$child('precourses').$child(ref.name()).$child('pairs').$child(afterCourse[key]).$set(afterCourse[key]);
});
});
在我的控制器中,我现在创建一个计划和日志结果:
$scope.schedule = Schedule.scheduleEvent($scope.eve);
$scope.schedule.$on('loaded', function() {
$scope.precourses = $scope.schedule.courses.precourses;
console.log($scope.precourses);
});
结果是:
Object {-JUyS4KEV3ljmNvmb8kL: Object, -JUyS4KHH9dM5qwhr_uX: Object, JUyS4KJOjjh5DGu3LSk: Object}
-JUyS4KEV3ljmNvmb8kL: Object
responsible: "-JUsWS58wX4Y_zEf7FqG"
__proto__: Object
-JUyS4KHH9dM5qwhr_uX: Object
-JUyS4KJOjjh5DGu3LSk: Object
__proto__: Object
Inside Precourse
知道我的承诺有什么不对吗? .then(服务)和$ on('已加载')之间可能存在冲突(控制器)
答案 0 :(得分:2)
您可能在从firebase加载数据之前记录console.log(value.pairs);
。如果升级到v0.8,则可以使用$loaded()
等待数据加载。使用您当前使用的版本,您可以使用$on("loaded")
e.g。
value.$on("loaded", function(loadedValue) {
console.log(loadedValue.pairs);
}