Reputation: 2084
Listening for updates to a collection in angularfire2 is easy enough. I want to also run a function whenever the new item is added to (or removed from) the collection. Here's some sample code::
let path = 'doc1/' + doc2 + '/doc3/';
this.varCollection = this.afs.collection(path);
this.var = this.varCollection.valueChanges(); //Do something here?
Upvotes: 1
Views: 82
Reputation: 11000
valueChanges()
returns an Observable so you can subscribe it as so:
this.subscription = this.varCollection.valueChanges().subscribe((val) => {
this.var = val;
runSomeFunc();
})
In case you use an async
pipe in your template or just want to keep Observable as it is, try to .map()
it and then return:
this.var = this.varCollection.valueChanges().map(_ => {console.log('value changed'); return _});
If you need document's metadata, can use snapshotChanges()
:
this.subscription = this.varCollection.snapshotChanges().subscribe( //do something )
Upvotes: 1