user2086656
user2086656

Reputation: 89

Angular's $watchCollection vs AngularFire's $watch

I am using angularfire with an array like this:

var ref = new Firebase(URL);
var sync = $firebase(ref);
$rootScope.list = sync.$asArray();

I want to know the difference between using angular's $watchCollection on 'list' and using angularfire built-in $watch like this:

$rootScope.list.$watch(function(event) {
  console.log(event);
});

Which one is faster and what are the use cases of each one?

Upvotes: 0

Views: 1055

Answers (1)

getglad
getglad

Reputation: 2562

You need to think about AngularFire as a service wrapper around the Firebase API. It augments and uses features of Angular, but ultimately is meant to help you integrate back to Firebase. That being said...

  • scope.$watchCollection() according to the spec "shallow watches the properties of an object and fires whenever any of the properties change". It works on arrays and object maps, but watches only those objects that are held by your client.

  • x.$watch in AngularFire is really more an abstraction of functions that enable the 'three way data bind' feature. Note that $watch is used for both $FirebaseObject and $FirebaseArray - it does different things in each context, but essentially enable you to keep your data in the client in sync with updates on your server/Firebase.

  • Considering the different roles, 'faster' is not really an appropriate question. If you are using $watch in AngularFire, you would not need to use $watchCollection on the same object. For what it's worth, AngularFire's $watch is actually pretty speedy.

Upvotes: 2

Related Questions