我如何通过火力地堡实例的子女环 [英] How do I loop through the children of a Firebase instance

查看:202
本文介绍了我如何通过火力地堡实例的子女环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道通过每个人的孩子怎么循环。我使用的火力地堡和AngularJS。

I want to know how to loop through the children of everyone. I'm using Firebase and AngularJS.

我的火力对象如下:

要我来说,它看起来像一本字典,所以从<一个href=\"http://stackoverflow.com/questions/558981/iterating-through-list-of-keys-for-associative-array-in-json\">Iterating通过在JSON关联数组键的列表我曾尝试

To me it looks like a dictionary, so from Iterating through list of keys for associative array in JSON I have tried

syncData('everyone').$bind($scope, 'everyone').then(function() {
  var keys = $scope.everyone.$getIndex();
  for (var key in $scope.everyone) {
   console.log("key : " + key + " value : " + $scope.everyone[key]);
  }
});

日志确实包含子对象,但它也包括所有的方法。像这样

The log does contain the child objects, but it also includes all the methods. Like so

... Before this line is all the other methods.
key : $on value : function (a,c){if("loaded"==a&&b._loaded)return b._timeout(function(){c()}),void 0;if(!b._on.hasOwnProperty(a))throw new Error("Invalid event type "+a+" specified");b._on[a].push(c)} controllers.js:58
key : $off value : function (a,c){if(b._on.hasOwnProperty(a))if(c){var d=b._on[a].indexOf(c);-1!==d&&b._on[a].splice(d,1)}else b._on[a]=[];else b._fRef.off()} controllers.js:58
key : $auth value : function (a){var c=b._q.defer();return b._fRef.auth(a,function(a,b){null!==a?c.reject(a):c.resolve(b)},function(a){c.reject(a)}),c.promise} controllers.js:58
key : $getIndex value : function (){return angular.copy(b._index)} controllers.js:58
key : -JH45WOOAtnZfUZkrJb1 value : [object Object] controllers.js:58
key : -JH45YdfwptGv3y6UqyV value : [object Object] controllers.js:58
key : -JH45_zxptV_dmibyGzL value : [object Object] 

有没有一种方法可以让我得到的只是孩子们?

我这样做,因为我的code被设计为使用数组,但火力地堡劝阻使用数组(对于多个人可以改变的值)。所以我通过火力词典试图环和对象​​复制到客户端上的一个阵列。所以,我没有太多的改变我的code的。

I'm doing this because my code was designed to use an array, but Firebase discourage using arrays (for values that multiple people could change). So I'm trying to loop through the firebase dictionary and copy the objects into an array on the client side. So I don't have to change too much of my code.

推荐答案

更新:由于AngularFire 0.8.4中,可以使用$ asArray()获得的记录排序数组,这个答案已经不再必要

在angularFire对象来迭代值正确的方法是使用$ getIndex()。您在code有这个上面,但没有利用它在for循环。

The correct way to iterate values in an angularFire object is by using $getIndex(). You have this in your code above, but did not utilize it in the for loop.

既然你已经在使用 angularFire LIB (syncData是的 angularFire种子使用angularFire服务),就没有必要担心调用$适用()或任何劝诱其他的复杂性数据到在previous答案角度详细(这是一个原始的火力地堡/角实现了良好的反响)。

Since you are already using the angularFire lib (syncData is the angularFire-seed service that uses angularFire), there is no need to worry about calling $apply() or any of the other complexities of coaxing data into Angular detailed in the previous answer (which is a good response for a raw Firebase/Angular implementation).

相反,只是改变你的for循环迭代的钥匙,而不是angularFire例如:

Instead, just change your for loop to iterate the keys instead of the angularFire instance:

syncData('everyone').$bind($scope, 'everyone').then(function() {
  var keys = $scope.everyone.$getIndex();

  // utilizing Angular's helpers
  angular.forEach(keys, function(key) {
     console.log(key, $scope.everyone[key]);
  });

  // or as a for loop
  for(var i=0, len = keys.length; i < len; i++) {
     console.log(keys[i], $scope.everyone[keys[i]]);
  }
});

要利用在DOM对象,使用 NG-重复

To utilize the object in the DOM, use ng-repeat:

<ul>
  <li ng-repeat="(key,user) in everyone">{{key}}, {{user|json}}</li>
</ul>

这篇关于我如何通过火力地堡实例的子女环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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