无法获取火力地堡对象的长度 [英] Unable to get length of Firebase Objects
问题描述
我目前使用AngularFire,并试图获得物体的长度在我的数据库。
I am currently using AngularFire, and trying to get the length of objects in my database.
在我火力地堡,结构看起来像
In my Firebase, the structure looks like
popping-fire-5575
celio
-JgaQt-tNq-gRVIVZdCD
artist:
track:
-JgaQuBoYk9VX3pWylx3
artist:
track:
-JgaQuf_pyBFJ7EA1Fo_
artist:
track:
在我的控制器,
var profileObject = FirebaseDemo.getBroadcast($routeParams.param);
var twotwo = profileObject.$asObject();
twotwo.$bindTo($scope, 'data');
当我控制台登录变量'twotwo',
我得到的回报
When I console log the variable 'twotwo', I get in return
Object
$$conf: Object
$id: "celio"
$priority: null
-JgaQt-tNq-gRVIVZdCD: Object
-JgaQuBoYk9VX3pWylx3: Object
-JgaQuf_pyBFJ7EA1Fo_: Object
__proto__: Object
不过,我已经尝试了所有不同的方式来获得的长度,但我没能获得成功。可能有人给我一些指示或建议吗?
However, I have tried all different ways to get the length, but I am not able to get to succeed. Could someone give me some directions or tips?
推荐答案
火力地堡负载(并同步),您数据不同步,因此受到时间的的console.log
语句运行它可能还在忙着装载数据。
Firebase loads (and synchronizes) you data asynchronously, so by the time your console.log
statement runs it is probably still busy loading the data.
幸运的是AngularFire有办法通知和运行code时的初始数据加载完成:
Luckily AngularFire has a way to notify and run your code when the initial loading of data has completed:
var twotwo = profileObject.$asArray();
twotwo.$loaded().then(function(data) {
console.log('Initial data loaded', data.length);
});
从code中的两个主要变化:
The two main changes from your code:
- 使用
$ asArray()
而不是$ asObject()
,因为你的数据结构是一个数组 - 收听的
$加载
事件,并于作出回应
- Use
$asArray()
instead of$asObject()
, since your data structure is an array - Listen for the
$loaded
"event" and respond to that
注意AngularFire就已经通知任何更改数据AngularJS,这样你就不必为 $加载
回应,如果你只是将数据绑定到 $范围
并显示在您的视图。
Note that AngularFire will already notify AngularJS of any changes to the data, so you won't have to respond to $loaded
if you just bind the data to the $scope
and show it in your view.
这篇关于无法获取火力地堡对象的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!