【发布时间】:2014-08-22 13:33:07
【问题描述】:
使用 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('loaded', 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
按下按钮再次重复forEach-loop:
(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 (service) 和 $on('loaded') (controller) 之间可能存在冲突
【问题讨论】:
-
$on 已在 AngularFire 0.8 中删除。我建议升级并使用新的 $asObject 和 $asArray 函数。调用 $asObject 将返回一个 $FirebaseObject,您可以遍历它的属性。
标签: angularjs firebase angularfire