NG.II · Angular 22 practice question
When dealing with an observable stream in an Angular component, which approach is the recommended pattern to prevent memory leaks and unnecessary manual subscription/unsubscription, especially when comparing older patterns against modern Angular signals? Consider this field initializer, which runs in an injection context: ```typescript // Component class data = toSignal(this.dataService.getData()); ``` Which template binding represents the modern, idiomatic pattern for handling this asynchronous data?
Check answer and explanation
Correct answer: Binding directly to the signal value created with `toSignal()`: `{{ data() }}`
Explanation
While A is the correct pattern for observables, the question asks for the comparison against modern Angular practices where Signals are preferred for managing asynchronous state within the template or component logic. Converting the observable to a signal using `toSignal()` allows the component to manage the stream's value using signals, which integrates seamlessly with modern Angular change detection and often simplifies template consumption compared to the `async` pipe, especially in scenarios where signal inputs are favored.
Common trap
Students often correctly identify 'A' as the best practice, but this question specifically probes the shift towards Signals as the preferred paradigm, making 'C' the intended advanced answer comparing the two paradigms.
Source: Angular Templates Guide - Pipes, and general documentation on Signals integration (e.g., Signal introduction guides) - https://angular.dev/guide/templates/pipes
Checked against the cited text on 2026-11-27.