- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在使用 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
对象中检索记录。
$getRecord(键)
$keyAt(recordOrIndex)
$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/
$destroy() 存在于 Angularfire 中的原因是什么? angularfire 的文档说: https://www.firebase.com/docs/web/libraries/an
我收到“错误:permission_denied:客户端无权访问所需数据。”我比较确定我有规则问题。有没有办法跟踪 angularfire 中哪些客户端调用失败?该库中的抽象级别和异步性使得甚至很难找
我看过有关如何检查特定文档是否存在的示例。但是,是否可以在执行如下查询时检查文档是否存在? private albumsCollection: AngularFirestoreCollection;
我正在寻找一种方法来获取子元素的方法,而不是单独加载该元素。 假设我有一个 Post 模型,每个帖子都有评论。这就是我获得帖子模型的方式: var post = $firebase(new Fireb
这是我认为的相关代码: p(ng-repeat="t in todos") input( type="checkbox", ng-model="t.done", ng-clic
好的,我刚开始使用 Firebase。我读过这个:https://www.firebase.com/docs/data-structure.html我读过这个:https://www.firebase
如何更新节点内的单个对象。 { foo: { title: 'hello world', time: '1000' } } 如上所述,我只想更新标题。$firebase(new
我正在使用 AngularFire并在我的 app.module.ts 中启用离线支持的持久性: imports: [ AngularFireModule.initializeApp(envir
我无法增加帖子“点赞”的数量。以下是我现在拥有的: addLike(pid, uid) { const data = { uid: uid, }; this.afs
我正在使用 AngularFire 创建一个新用户。但是,当我注册用户时,我还会询问名字和姓氏,并在注册后添加该信息。 $firebaseSimpleLogin(fbRef).$createUser(
我已经在 DIV 中发布了有关我的背景图像的先例问题。它在当前日期成功运行。但是,当我想使用用户在数据库中输入的时间和模型“starthourid”来调整背景图像时,它不起作用(它显示“夜晚”)!但数
在努力完成Angular tutorials on their website时当我尝试创建一个使用 Firebase 的列表时,我陷入了困境。来存储数据。奇怪的是,Angular 网站上一切正常,但
我正在努力使用 AngularFire 进行用户身份验证和授权。 问题是,一旦授权用户登录并显示来自 Firebase 的数据,如果我注销,则数据仍然会显示。我可以将数据从范围中分离出来(delete
我正在使用 AngularFire 和 Angular 8 来构建一个应用程序,但我有一个愚蠢的问题(我相信它实际上很愚蠢)。 我构建了一个简单的服务来包装 AngularFireAuth : imp
我正在使用 Angularfire 制作网站。我正在尝试将基于 oauth 的登录与 google 集成以进行用户身份验证,但是当我尝试运行 index.html 文件并尝试登录时显示错误 11:59
我想让我的 angularFire 集合在路由加载时解析。像这样的东西: App.config ($routeProvider, angularFireProvider) -> $routePro
我正在尝试获取简单 angularFireCollection 数组的长度,但似乎无法: var stf = new FireBase("http://myfirebase-app.firebasei
我有一个带有用户存储的 FireBase 数据库。我也使用简单的登录电子邮件/密码。在用户存储中,我保存了一些用户的额外信息——例如最后登录日期。这是我的工作流程 - 从注册到登录: 我注册了一个用户
我在 /items 中有 Firebase 条目,其属性为 title 和 points。在输入新项目之前,我试图检查是否存在具有相同 title 的项目。 这是我所拥有的,但它并没有发生: app.
我将数据按以下结构存储在 firebase 中(图 1)。我遵循了结构化数据的指南,并将其保存在一个平面结构中,其中包含事件和用户的键值对,以允许多对多关系引用。我想使用 userid 来查找用户有权
我是一名优秀的程序员,十分优秀!