NG.I · Angular 22 practice question
A component uses `@ViewChildren` to gather references to several child elements. The developer needs to execute a specific side effect immediately whenever the set of referenced elements changes. Which mechanism should the developer utilize?
import { Component, ViewChildren, QueryList } from '@angular/core';
@Component({
selector: 'app-parent',
template: `<div><app-child></app-child><app-child></app-child></div>`
})
export class ParentComponent {
@ViewChildren(ChildComponent) children!: QueryList<ChildComponent>;
ngAfterViewInit() {
// Logic here
}
}
Check answer and explanation
Correct answer: Subscribe to the `changes` property of the `QueryList` to trigger logic whenever the collection is modified.
Explanation
The correct approach is to "Subscribe to the `changes` property of the `QueryList` to trigger logic whenever the collection is modified," as this is the documented method for reacting to dynamic updates.
Common trap
Calling `toArray()` only captures the list's state at that exact moment, not future changes. Using `map` or `reduce` processes the current data but doesn't set up a listener for future modifications.
Source: Angular docs: Referencing component children with queries — Using QueryList
Checked against the cited text on 2026-11-25.