arrays - $firebaseArray 中的 For 循环

标签 arrays angularjs loops firebase angularfire

我发现了一些类似的主题,但其中大多数已经过时了,因为我使用的是 Angularfire 1.1.1。

我有一个 AngularJS 应用程序在 Google map 上创建标记。数据存储在外部 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 版本记录同步对象如下所示:

    [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);
    });

我也无法访问数组的属性,因此无法循环遍历它。

最佳答案

看起来 $loaded() 方法不起作用。我在加载数据和执行其他操作之间添加了 2 秒超时,因此它开始工作。

关于arrays - $firebaseArray 中的 For 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30758243/

相关文章:

php - 从 mysql 表获取行到 php 数组并将其传递给 ajax

javascript - 为什么传递给 for 循环内的函数的 'i' 等于大于 'i' 的数字?

javascript - Angular ng-controller 似乎在 route 不起作用

angularjs - Angular 中的动态标签生成

JavaScript 多个循环

javascript - 在 javascript 24 小时倒计时中使用所需的时区或偏移倒计时,使其达到 00 :00 at 11:00 local time instead of at 00:00 local time

python - 从python中的n个子数组副本创建一个数组

javascript - Google 更新和 AngularJS/JavaScript 网站

python - 如何使用 Python 组合来自不同数组的 JSON 对象

c - 从文件读取字符串并将它们复制到数组中 - C