【问题标题】:How to get the object key when using value in angularFire在angularFire中使用值时如何获取对象键
【发布时间】:2014-05-20 07:50:15
【问题描述】:

我有以下代码从users node 查询索引,然后将索引传递给todos node 以获取我想要的数据,但只有watching 添加了一个孩子,所以当一个孩子从索引中删除时不更新视图。

如果我在索引节点中使用value,我不知道如何获取对象键。 任何的想法?还是使用 angularFire 查询主控数据的更好方法?

FirebaseRef.$child(user.uid).$on('child_added', function(a) {
 FirebaseRef.$child('todos/' + a.snapshot.name).$on('value', function(b) {
   $scope.todoList[a.snapshot.name] = b.snapshot;
 });
});

【问题讨论】:

  • 什么是 FirebaseRef? Firebase 实例还是 $firebase/angularFire 对象?如果它是 Firebase 对象,请使用 .child 而不是 .$child。假设它是一个 $firebase 实例,那么它已经包含了 todos/ 的所有内容。我们在这里试图解决的用例是什么?看起来这可以用更简单的方式解决。

标签: firebase angularfire


【解决方案1】:

尝试以下(未测试)。

FirebaseRef.$child(user.uid).$on('child_added', function(a) {
 FirebaseRef.$child('todos/' + a.name()).$on('value', function(b) {
   $scope.todoList[a.name()] = b.snapshot;
 });
});

您的引用中不需要“快照”,例如 a.snapshot.name,您应该只引用回调函数中返回的对象

【讨论】:

  • AngularFire 不返回纯 firebase js api 之类的方法。它返回一个对象列表,所以我不能在 angularFire 中使用 .name() .val()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-03
  • 1970-01-01
  • 1970-01-01
  • 2015-07-29
  • 1970-01-01
  • 2020-09-02
相关资源
最近更新 更多