【问题标题】:AngularFire Watch and Retrieve Item KeyAngularFire 监视和检索项目密钥
【发布时间】:2015-08-22 09:19:18
【问题描述】:

我正在尝试查看我的 firebase 数组是否有更改,并在更改生效时调用一个函数。

我已经实现了这一点,但是我无法获得 firebase 用来存储数据的 key,因此我可以将其传递给我正在调用的函数。

键是-JrFJO3-vJ8DBHovapub 或像estimates 这样的节点的最顶层子节点

estimates
{
   -JrFJO3-vJ8DBHovapub
  {
     ....
  }
  ....
}

我当前的代码如下所示:

$scope.estimates.$watch(function(data) {
   if(data.event == "value") {
      angular.forEach($scope.estimates, function(estimate) {
         es.findTotal();
      });
   }
});

是我现在拥有的。调用value事件时如何获取key?

【问题讨论】:

  • 您的代码未显示您如何创建$scope.estimates。但如果它是$firebaseArray,您可能希望按照此处记录的方式对其进行扩展:firebase.com/docs/web/libraries/angular/guide/…
  • 我要做的就是让编辑后的密钥能够调用需要密钥才能工作的函数。如果那会让它工作。然后把它放在一个答案中。这个网站的重点。

标签: angularjs firebase angularfire


【解决方案1】:

为什么不使用新值data,而不是$scope.stimates

$scope.estimates.$watch(function(data) {
    if(data.event == "value") {
        angular.forEach(data, function(estimate, KEY) {
            console.log(KEY); //estimate key
            es.findTotal();
        });
    }
});

【讨论】:

  • 因为console.log(KEY) 显示单词event
  • 不是-JrFJO3-vJ8DBHovapub 作为我的问题中的示例。
猜你喜欢
  • 1970-01-01
  • 2018-08-21
  • 2015-11-15
  • 2017-03-07
  • 1970-01-01
  • 1970-01-01
  • 2013-09-20
  • 2020-08-20
  • 1970-01-01
相关资源
最近更新 更多