← Back to Angular, page 6

angular-0116 · Easy fresher

What is the difference between viewChild, contentChild and their plural forms, and when are the results available?

viewChild queries elements and components in the component's own template, while contentChild queries content that a parent projected between the component's tags. The plural forms return all matches. The signal versions update automatically and are ready after the first render (view) or after content init (content).

Full answer

Queries let a component get references to things in its template or content.

viewChild finds an element, component, or directive declared in the component's own template. Use a template reference name or a class: viewChild('box'), viewChild(ChartComponent). viewChildren returns all matches as a signal of an array.

contentChild finds things that the parent projected into this component through <ng-content>. If a tab group component is used as <app-tabs><app-tab /><app-tab /></app-tabs>, then contentChildren(TabComponent) in the tab group finds the tabs, because they are content, not part of its own view. A viewChild would not find them.

Signal queries (stable since Angular 19):

  • viewChild<ElementRef>('box') returns Signal<ElementRef | undefined>.
  • viewChild.required(...) returns Signal<T> and throws if there is no match.
  • contentChild, contentChildren and viewChildren follow the same pattern.
  • Because they are signals, they work in computed and effect, and update when @if or @for adds or removes matches.
  • Options: { read: ElementRef } picks what to return, { descendants: true } includes nested content children.

When are results available? Signal queries return undefined (or an empty array) before the first render, so read them in effect, afterNextRender, or a lifecycle hook after init. Content queries are ready after ngAfterContentInit, and view queries after ngAfterViewInit.

The older decorators (@ViewChild, @ContentChild) still work, but have a static flag and are not reactive. Prefer the signal versions in new code.

A common mistake is using viewChild for projected content, or reading a query in the constructor.

typescript
import { Component, ElementRef, computed, contentChildren, viewChild } from '@angular/core';

@Component({ selector: 'app-tab', template: `<ng-content />` })
export class TabComponent {}

@Component({
  selector: 'app-tabs',
  template: `
    <h2 #heading>Tabs ({{ count() }})</h2>
    <ng-content />
  `,
})
export class TabsComponent {
  readonly tabs = contentChildren(TabComponent); // projected by the parent
  readonly heading = viewChild.required<ElementRef<HTMLElement>>('heading'); // own template
  readonly count = computed(() => this.tabs().length);
}

@Component({
  selector: 'app-page',
  imports: [TabsComponent, TabComponent],
  template: `<app-tabs><app-tab>One</app-tab><app-tab>Two</app-tab></app-tabs>`,
})
export class PageComponent {}

Angular 20.3, strict template check passes. A Karma test checks that the tab group counts the projected tabs and the view query finds its heading.

Key points

  • `viewChild` is for your own template. `contentChild` is for projected content.
  • Plural versions return all matches as a signal of an array.
  • Signal queries are reactive. Use `.required` when a match must exist.
  • Results are not ready in the constructor. Use hooks, `effect`, or `afterNextRender`.

Common mistakes

  • Using `viewChild` to find components projected by a parent.
  • Reading the query in the constructor, where it is still undefined.
  • Forgetting `descendants: true` for nested content children.

Follow-ups

  • What does `read` do in a query?
  • How does `viewChild` behave inside an `@if`?
  • When would you use `contentChildren` in a component library?

Tips

  • Give the tab group example for content queries.
  • Mention signal queries and `.required`.
  • State when each result is ready.

Evaluation rubric

  • Distinguishes view and content queries.
  • Knows signal query APIs and timing.
  • Avoids the common mistakes.