Dan Kanze
Dan Kanze

Reputation: 18595

angularFireCollection callback Firebase

What if I don't want to directly assign my collection like this:

$scope.items = angularFireCollection(ref);

Because I need to access the object in a callback once it's received:

angularFireCollection(ref, function(items) {
  angular.forEach(items, function(item){
    angular.forEach(item.tags, function(tag){
      $scope.allTags.push(tag);
    });    
  });
  $scope.items = items;
});

Does a callback method exist for the angularFireCollection service?

Upvotes: 0

Views: 218

Answers (2)

Dan Kanze
Dan Kanze

Reputation: 18595

Use .val() as an extension of the object returned by the callback to access it's value.

$scope.items = angularFireCollection(ref, function(i){
    console.log(i.val());
});

JSFiddle

Upvotes: 0

hiattp
hiattp

Reputation: 2336

Yes there is, from the documentation on explicit binding:

angularFireCollection takes an optional second argument - a callback that will be invoked when the initial data has been loaded from the server. The callback will be provided with a Firebase snapshot as the only argument.

So you could call another function in the callback that will iterate over $scope.items directly to load the $scope.allTags array, or do something like you are now but use the snapshot instead (e.g. use the forEach() function).

Upvotes: 1

Related Questions