[FIXED] Angular Observables Unsubscribe async pipe

Issue

I’m building an App using Angular + Ionic.

When I present the data in an observable in my template using the async pipe.

My understanding is that Angular automatically unsubscribe from the observable when the component is destroyed, but, if I move to another page, the ngOnDestroy event is not being fired. Does it mean that I’m not unsubscribing from the observable?

Thanks!

Solution

unsubscribe and ngOnDestroy are 2 different concepts :

  • unsubscribe closes an observer stream : the subscription won’t listen to new events
  • ngOnDestroy is ran when a directive is destroyed (removed from the DOM)

async pipes are automatically unsubscribing from the observables they’re fed with : but that isn’t because of the ngOnDestroy function.

If your function doesn’t get triggered, it either means that you don’t destroy your component, or that you have an issue.

If so, please provide a sandbox reproducing it, for instance on https://stackblitz.com

Answered By – user4676340

Answer Checked By – David Goodson (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published