Do I need to unsubscribe each time onDestroy in component?
Just an additional hint to the Pierre's great answer. The async
pipe automatically unsubscribes observables it's applied on. So you need to unsubscribe for observables you manage by your own.
See this line in source code:
- https://github.com/angular/angular/blob/master/modules/angular2/src/common/pipes/async_pipe.ts#L74
I found it! And yes, per documentation as stated in the angular2 lifecycle-hook
guide:
ngOnDestroy
Cleanup just before Angular destroys the directive/component. Unsubscribe observables and detach event handlers to avoid memory leaks.
So yes, you need to unsubscribe from observables on ngOnDestroy()