I am new to Angular and RxJs and Ngrx
I know that we subscribe to an Observable and watch for changes. I came across this code in Ngrx getting started
<div>Current Count: {{ count$ | async }}</div>
Question is what exactly is AsyncPipe and how its different from subscribing to an Observable ? and when is one used and when the other?
As @jonrsharpe mentioned it isn't really different. Under the hood the async pipe will create a subscription and store the most recent value which is the same thing that you would need to do if you wanted to subscribe and display the result.
The async
pipe will also take care of unsubscribing from the observable when the component (the directive) is destroyed.
It may be slightly more efficient in terms of change detection but I'm not certain.
Mostly however, it is just used for convenience. It is less code to use an async
pipe than it is to create a component variable and subscribe to it in the component's onInit
or constructor and then keep track of the subscription to unsubscribe.