NG.I · Angular 22 practice question

A developer is creating a parent component that renders several instances of a child component. To process all of these child instances once the view is initialized, which decorator and lifecycle hook should the developer utilize?

import { Component, ViewChildren } from '@angular/core';

@Component({ /* ... */ }) 
export class ParentComponent {
  @ViewChildren(ChildComponent) children!: QueryList<ChildComponent>;

  ngAfterViewInit() { /* logic */ }
}
Choose one answer
Check answer and explanation

Correct answer: Employ @ViewChildren() to retrieve a QueryList and process the collection in ngAfterViewInit().

Explanation

The correct approach is to 'Employ @ViewChildren() to retrieve a QueryList and process the collection in ngAfterViewInit()', because the source indicates that '@ViewChildren' creates a QueryList object and results are available in 'ngAfterViewInit'.

Common trap

Using the constructor is incorrect because view queries are not available until after the view is rendered. Relying on ngOnInit() is wrong because the source specifies that view query results become available in 'ngAfterViewInit'.

Source: Angular docs: Referencing component children with queries — View queries {#decorator-view-queries}
Checked against the cited text on 2026-11-25.

Report an error in this question