Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularFire2 Observer

The AngularFire2 documentation demonstrates the following pattern to bind an observer to your Firebase observable:

this.item = af.database.object('/item');

{{ (item | async)?.name }}

The async pipe unsubscribes when the component is destroyed, but what are the advantages/disadvantages of using the following pattern instead? And unsubscribing in the lifecycle hook ngDestroy?

af.database.object('/item')
.subscribe(item => this.item = item)

{{ item?.name }}
like image 729
J. Adam Connor Avatar asked Apr 22 '26 01:04

J. Adam Connor


1 Answers

Advantage: You can directly display the list to the template without subscribing it. It will save you time referencing it to a property. This pattern is used not only in AngularFire2 but in Angular 2 also. This is how Observable works.

Disadvantage: You can't manipulate the data within since you already displayed it directly. If you want to change something to the properties you need to use a pipe which take you time to create the logic.

like image 113
Calvin Ferrando Avatar answered Apr 23 '26 18:04

Calvin Ferrando