angularfire - 为什么可以在阵列不是我通过循环$ asArray回来了? [英] angularfire - why can't I loop over array returned by $asArray?

查看:145
本文介绍了angularfire - 为什么可以在阵列不是我通过循环$ asArray回来了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用AngularFire 0.8

I'm using AngularFire 0.8

我叫下面的函数来获得一个数组的一个实例。

I called the following function to get an instance of an array.

var test = $firebase(new Firebase(URL)).$asArray();

当我CONSOLE.LOG'测试',我得到以下(见截图)。

When I console.log 'test', I get the following (see screenshot).

它看起来像测试是一个数组。然而,当我做的console.log(测试[0]),我得到了一个未定义。

It looks like test is an array. However, when I do "console.log(test[0])", I get undefined.

为什么没有像执行console.log(测试)两个元素测试一个数组似乎表明它应该是???

Why isn't test an array with two elements like console.log(test) seems to show it should be???

帮助!

推荐答案

最有可能在加载数据之前你调用的console.log 阵列上。
要访问数组中的数据,你必须等待,直到数据被加载。

Most likely you're calling console.log on the array before the data is loaded. To access the data in the array, you have to wait until the data is loaded.

要做到这一点在code,在使用此结构:

To do that in code, you use this construct:

var test = $firebase(new Firebase(URL)).$asArray();
test.$loaded().then(function(array) {
    console.log(array[0]);
});

更新

这个答案得到更多upvotes比我认为这是值得的。当你使用AngularJS和AngularFire,它往往是一个不好的预兆,当您使用的console.log 来调试加载的数据。

Update

This answer gets more upvotes than I think it's worth. When you're using AngularJS and AngularFire, it is often a bad omen when you use console.log to debug the data that is loaded.

的<一个href=\"https://www.firebase.com/docs/web/libraries/angular/guide/intro-to-angularfire.html#section-async-intro\"相对=nofollow> AngularFire关于处理异步操作文档现在已经在其上说:

要记录数据的最简单方法是在视图中打印出来的 的采用了棱角分明的 JSON 过滤器。

The easiest way to log the data is to print it within the view using Angular's json filter.

{{ data | json }}

AngularFire告诉编译器角时,它已经完成加载数据,所以没有必要担心时可用它。

AngularFire tells the Angular compiler when it has finished loading the data, so there is no need to worry about when it be available.

这篇关于angularfire - 为什么可以在阵列不是我通过循环$ asArray回来了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆