What does the async pipe do, and why is it recommended?
Model answer
The async pipe subscribes to an Observable or a Promise in the template and shows its latest value. It also unsubscribes automatically when the component is destroyed, so you do not forget to clean up, and it marks the view for update when new values arrive.
Example: @if (users$ | async; as users) { ... }. It reduces manual subscribe code and prevents memory leaks. Remember that each use in a template creates its own subscription, so use "as" to share the value.
Learn this topic
Angular Tutorial: Beginner to Advanced .
Sign in to add your own answer.
More questions
- What is a directive? What are the types?
- What is the difference between ngClass and ngStyle, and how do they compare with class and style bindings?
- What is a pipe? Name some built-in pipes.
- How do you create a custom pipe? What is the difference between pure and impure pipes?
- How do you build a custom attribute directive?
- What is the difference between providedIn root and providing a service in a component?