Search code examples
javascriptangularjsfirebaseangularfire

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?


Solution

  • 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).