3
votes

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!

1
Async pipe with ng-container automatically unsubscribes from the declared observable.Aditya Yada

1 Answers

7
votes

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