AngularFire / Firebase - 从$ firebaseArray中获取单个元素以便在DOM / $中使用

时间:2015-03-11 21:01:16

标签: angularjs firebase angularfire

我在使用AngularFire / Firebase从集合中获取单个记录时遇到了一些麻烦(然后,在我拥有它之后,从集合中删除它)。我相信我正在遵循此处的正确文档:https://www.firebase.com/docs/web/libraries/angular/api.html#angularfire-firebasearray

注意:我发现很多文档/博客/答案似乎都使​​用了弃用的$ firebase服务(使用$ asArray方法) - 但是,更新的文档确定$ firebaseArray是正确使用的服务,FYI。

出于某种原因,即使我可以准确地提取我的民意调查集合,我也无法提取单个元素(尽管官方文档看起来与我正在使用的语法相同)。

$ remove文件(https://www.firebase.com/docs/web/libraries/angular/api.html#angularfire-firebasearray-removerecordorindex):

## official docs ##

$remove(recordOrIndex)
Remove a record from Firebase and from the local data. This method returns 
a promise that resolves after the record is deleted at the server. It will 
contain a Firebase reference to the deleted record. It accepts either an 
array index or a reference to an item that exists in the array.

var list = $firebaseArray(ref);
var item = list[2];
list.$remove(item).then(function(ref) {
  ref.key() === item.$id; // true
});

我的代码(请参阅下面的console.log):

.service('pollsData', function($firebaseArray){
    var fireRef = new Firebase('<-my-firebase-ref-url>');
    var polls = $firebaseArray(fireRef.child('polls'));
    var service = this;

    this.clearPolls = clearPolls;
    this.setInitialPolls = setInitialPolls;
    this.getPolls = getPolls;

    function clearPolls() {
      console.log("length: ", polls.length);  <--- I get a length of 0
      console.log(polls);  <-- I get a $firebaseArray object including the methods in the API docs
      console.log(polls[0], polls[1], polls[2]); <--- all 3 are undefined even though I can see several in the firebase data dashboard

      var poll = polls[1];
      console.log("poll?: ", poll);
      polls.$remove(poll);
    }

    function getPolls() {
      polls = polls || $firebaseArray(fireRef.child('polls'));
      return polls;  <-- I can successfully ng-repeat over this returned selection in the template
    }

    function setInitialPolls() {
      service.clearPolls();
      polls.$add({
        one: {
          name: "fantastic pollllllls",
          selection_one: {
            name: "pizza",
            count: 0
          },
          selection_two: {
            name: "fries",
            count: 0
          }
        },
        two: {
          name: "mediocre poll",
          selection_one: {
            name: "blue",
            count: 0
          },
          selection_two: {
            name: "green",
            count: 0
          }
        }
      });
    }

    setInitialPolls();
});

有关为什么括号[]符号从$ firebaseArray中取出记录的任何见解都无法按照AngularFire文档的预期工作?

更新:提供的回复仍然有用,因为这似乎与我对$ firebaseArray的新文档的理解以及API文档中$ remove部分的示例相冲突。了解这仍然是一个异步问题,但由于$ firebase服务已被弃用,而且我引用了更新的文档,因此需要更新此问题/解决方案。

1 个答案:

答案 0 :(得分:2)

有三种方法可以从$firebaseArray对象中检索记录。

  1. $getRecord(key)
  2. $keyAt(recordOrIndex)
  3. $indexFor(key)
  4. 我会更仔细地查看每个文档,但是根据您的描述,您可能希望使用$keyAt。但请记住,$firebaseArray不是一个严格的数组,并且可用的方法提供的选项/灵活性比您在“只是一个数组”中所用的方法更多。

    此外,正如评论中所提到的,您希望在promise方法内部处理三方数据绑定。

    var arrayRef = new Firebase(firebase_location).child(child_location);
    var theList = $firebase(arrayRef).$asArray();
    theList.$loaded(function (list) {
      var x = list.$keyAt(list[target_position]);
    
      //use other methods for the $firebaseArray object
    });
    
相关问题