gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:39:58 25 4
gpt4 key购买 nike

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

注意:我发现的很多文档/博客/答案似乎都在使用已弃用的 $firebase 服务(使用 $asArray 方法)——但是,最近的文档表明 $firebaseArray 是正确的服务,仅供引用。

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

$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 服务已被弃用并且我正在引用更新的文档,所以这个更新的问题/解决方案感觉是必要的。

最佳答案

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

  1. $getRecord(键)
  2. $keyAt(recordOrIndex)
  3. $indexFor(键)

我会更仔细地查看每一个的文档,但根据您的描述,您可能想要使用 $keyAt。不过请记住,$firebaseArray 不是严格的数组,可用的方法提供了比您习惯于“只是一个数组”的方法更多的选项/灵 active 。

此外,如评论中所述,您希望在 promise 方法内部工作,为您处理三向数据绑定(bind)。

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
});

关于angularjs - AngularFire/Firebase - 从 $firebaseArray 获取单个元素以便在 DOM/$remove 中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28997282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com