NG.II · Angular 22 practice question
In an Angular component utilizing `ChangeDetectionStrategy.OnPush` and employing signals for state management, when should you use `computed()` versus directly accessing the value of a signal within the template (e.g., `{{ mySignal() }}`) to ensure the component updates correctly?
// Component context
state = signal('initial');
derivedState = computed(() => this.state().toUpperCase());
Check answer and explanation
Correct answer: Both `computed()` and direct signal access will trigger change detection for the component if the state signal changes, making there no functional difference in this scenario.
Explanation
When using `ChangeDetectionStrategy.OnPush`, Angular only checks components whose inputs have changed or whose observable dependencies have emitted. Signals, when used correctly (especially via `computed()`), provide a built-in mechanism to signal reactivity. While accessing a signal in a template might work if the component tree is being checked anyway, using `computed()` explicitly creates a derived signal whose value depends on the source signal. This explicit dependency declaration allows Angular's change detection to recognize that the derived state *must* be re-evaluated and potentially trigger updates even under `OnPush` conditions, mirroring the dependency tracking behavior expected in other reactive patterns.
Common trap
Learners might assume that because signals inherently manage reactivity, the difference between calling a signal accessor versus wrapping it in `computed()` vanishes under `OnPush`.
Source: Signals documentation regarding reactivity and change detection (https://angular.dev/guide/signals)
Checked against the cited text on 2026-11-27.