AngularFire Watch and Retrieve Item Key

158 views Asked by At

I am trying to watch my firebase array for changes and call a function when the changes take effect.

I have achieved this however, I am not able to get the key that firebase uses to store the data so I can pass it onto my function I am calling.

The key being -JrFJO3-vJ8DBHovapub or the top most child of a node like estimates

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

My current code looks like such:

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

Is what I have right now. How do I obtain the key when a value event is called?

1

There are 1 answers

2
Walter Chapilliquen - wZVanG On

Why not use the new value data, instead of $scope.stimates?

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