【发布时间】:2015-08-25 19:37:27
【问题描述】:
我发现了一些类似的主题,但大多数都已经过时了,因为我使用的是 angularfire 1.1.1。
我有一个 AngularJS 应用程序在 Google 地图上创建标记。数据存储在外部 JSON 文件中,当我将其转换为数组时它工作得很好。当我将数据放入 firebase 数据库时,它停止工作。带有数据的 ng 指令可以正常工作,但是在 js 代码中访问数组数据数据时,它并没有真正起作用。
app.controller('MapCtrl', ['$scope', '$firebaseArray', function($scope, $firebaseArray){
var ref = new Firebase("https://radiant-inferno-6439.firebaseio.com/tracks");
var syncObject = $firebaseArray(ref);
...
使用旧的离线 JSON 版本记录,syncObject 看起来像这样:
[Object, Object, Object]
0: Object
1: Object
2: Object
length: 3
__proto__: Array[0]
但现在看起来是这样的:
[]
0: Object
1: Object
2: Object
$$added: () { [native code] }
$$error: () { [native code] }
$$getKey: () { [native code] }
$$moved: () { [native code] }
$$notify: () { [native code] }
$$process: () { [native code] }
$$removed: () { [native code] }
$$updated: () { [native code] }
$add: () { [native code] }
$destroy: () { [native code] }
$getRecord: () { [native code] }
$indexFor: () { [native code] }
$keyAt: () { [native code] }
$loaded: () { [native code] }
$ref: () { [native code] }
$remove: () { [native code] }
$save: () { [native code] }
$watch: () { [native code] }
length: 3
__proto__: Array[0]
为什么会这样?当我打印数组的长度时,它显示为 0。我尝试了 $loaded 函数,但它不影响。
syncObject.$loaded().then(function(syncObject) {
console.log(syncObject.length);
});
我也无法访问数组的属性,所以无法循环遍历它。
【问题讨论】:
标签: arrays angularjs loops firebase angularfire