加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – angularfire – 为什么我不能循环$asArray返回的数

发布时间:2020-12-17 07:51:51 所属栏目:安全 来源:网络整理
导读:我正在使用AngularFire 0.8 我调用以下函数来获取数组的实例. var test = $firebase(new Firebase(URL)).$asArray(); 当我在console.log“test”时,我得到以下内容(见截图). 看起来test是一个数组.但是,当我执行“console.log(test [0])”时,我得到了未定义.
我正在使用AngularFire 0.8

我调用以下函数来获取数组的实例.

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

当我在console.log“test”时,我得到以下内容(见截图).

看起来test是一个数组.但是,当我执行“console.log(test [0])”时,我得到了未定义.

为什么不测试一个带有两个元素的数组,比如console.log(test)似乎表明它应该是???

帮帮我!!

很可能你在加载数据之前在数组上调用console.log.
要访问数组中的数据,您必须等到数据加载完毕.

要在代码中执行此操作,请使用以下构造:

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

更新

这个答案得到的回报比我认为值得多.当您使用AngularJS和AngularFire时,使用console.log调试加载的数据通常是一个不好的预兆.

AngularFire documentation on handling asynchronous operations现在不得不说:

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

06001

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读