Automatically Subscribe and Unsubscribe an Observable in Angular


How to auomatically subscribe and unsubscribe an Observable in Angular

There are several ways to unsubscribe the Observable in Angular but best way to unsubscribe is using async pipe.
The async pipe subscribes to an Observable or Promise and returns the latest value it has emitted. When the component gets destroyed, the asyncpipe unsubscribes automatically to avoid potential memory leaks.

template: `
Interval: {{observable$ | async}}
export  class AppComponent implements OnInit {
ngOnInit () {
this.observable$ = Rx.Observable.interval(1000);
Next Post Previous Post
No Comment
Add Comment
comment url