NG.II · Angular 22 practice question
In an Angular 22 component, you need to read the value of a child component's input property immediately after the view has initialized, but the property value depends on asynchronous data loading within the parent component. Which approach should you avoid when using `viewChild()` or `viewChildren()` with signals?
Check answer and explanation
Correct answer: Using `viewChild` and accessing the property directly in `ngOnInit`, which will likely result in `undefined`.
Explanation
Relying on direct access within `ngOnInit` via `viewChild()` is problematic if the required data hasn't arrived yet, as the view query executes too early relative to data fetching. While signals offer better reactivity, the fundamental issue remains: synchronous API access before async data availability leads to stale or undefined states. The correct pattern is to handle the asynchronous data flow first, and *then* access the component or its properties when the data is ready.
Common trap
Developers might assume that because signals provide reactivity, a simple `viewChild` call in `ngOnInit` will automatically re-evaluate when the dependency changes.
Source: Components and View Queries documentation, https://angular.dev/guide/components/queries
Checked against the cited text on 2026-11-27.