ThisCalculator

Everyday

Angular Child Queries: View or Content?

Updated · Standard formula, shown below

Average pace 5.00 min/km
Average speed
12.00 km/h
Result
Finish time
50 min
You entered
Distance
10 km
You entered

Angular child queries let you grab references to child components, directives, or DOM elements from a parent class. The short answer: use view queries (@ViewChild/@ViewChildren) for elements inside your component’s own template, and content queries (@ContentChild/@ContentChildren) for elements projected into your component via <ng-content>. This distinction is fundamental to Angular’s component model, and choosing the wrong one is a common source of “undefined” errors.

Formula

For the pace calculator above, the formulas are:

  • Average pace (min/km) = finish time (minutes) ÷ distance (km)
  • Average speed (km/h) = distance (km) ÷ (finish time (minutes) ÷ 60)

Variables: t = finish time in minutes, d = distance in kilometers. Pace is expressed as minutes per kilometer; speed is kilometers per hour.

Worked Example

Using the defaults: t = 50 min, d = 10 km.

  1. Pace = 50 ÷ 10 = 5.00 min/km
  2. Speed = 10 ÷ (50/60) = 10 ÷ 0.8333 = 12.00 km/h

The calculator returns pace 5.00 min/km and speed 12.00 km/h.

Background: The Component Tree

Angular components form a tree. Each component has its own template (the HTML it renders) and an optional content area where a parent can insert children. View queries operate on the component’s own template—the elements you wrote directly in that component’s HTML. Content queries operate on the projected content—the elements a parent places between the component’s opening and closing tags, which Angular routes to an <ng-content> slot.

This separation mirrors the difference between a component’s internal structure and its public API for content projection. A card component, for example, might have a header inside its own template (view) and accept custom footer content from the parent (content).

How View Queries Work

@ViewChild and @ViewChildren are decorators that query the component’s view. They are resolved after the view is created, before ngAfterViewInit runs. You can query by:

  • Component class: @ViewChild(MyComponent) comp: MyComponent;
  • Directive class: @ViewChild(MyDirective) dir: MyDirective;
  • Template reference variable: @ViewChild('myRef') el: ElementRef;

If the element is inside an *ngIf or *ngFor, the query result can be undefined until the condition becomes true. Use @ViewChildren with a QueryList to react to changes, or set static: true only when the element is always present and does not depend on data binding.

How Content Queries Work

@ContentChild and @ContentChildren query the projected content. They are resolved before ngAfterContentInit. The same query types apply, but the source is the content that the parent passes in. For example, a tab component might use @ContentChildren(TabItem) to get all the tab items the user placed inside it.

Content queries are essential for building reusable, composable components. They allow a parent to supply arbitrary content while the child controls how it is displayed and managed.

Choosing the Right Query

Ask: Is the element I need written in my own template, or is it provided by a parent? If it’s in your template, use view queries. If it’s projected via <ng-content>, use content queries. A practical test: if you can see the element in the component’s HTML file, it’s a view query. If you expect the parent to put it between your component’s tags, it’s a content query.

Common scenarios:

  • Accessing a child component that you always render—view query.
  • Accessing a child component that the parent optionally passes—content query.
  • Getting a DOM element with a template ref—view query.
  • Getting all projected list items—content query.

Common Problems and Misconceptions

A frequent error is getting undefined when trying to access a view child in ngOnInit. View children are not available until ngAfterViewInit. Similarly, content children are not ready until ngAfterContentInit. Another misconception is that static: true solves all timing issues—it only works for elements that do not depend on structural directives or data bindings. For dynamic content, rely on the lifecycle hooks or subscribe to QueryList.changes.

Another pitfall: using a view query to access a projected element. The element is not part of the child’s view, so the query returns nothing. Conversely, using a content query to access an element in the child’s own template fails for the same reason.

Comparisons and Alternatives

Angular also provides @ViewChildren and @ContentChildren for multiple results, returning a QueryList that updates when the DOM changes. If you need to communicate between a parent and child, you might not need queries at all—use @Input() and @Output() for simple data flow, or a service for shared state. For DOM manipulation, consider ElementRef with caution and prefer Angular’s built-in mechanisms.

Frequently Asked Questions

Why is my @ViewChild undefined?

Most likely because you’re accessing it before ngAfterViewInit or the element is inside an *ngIf that hasn’t rendered yet. Move your logic to ngAfterViewInit or use static: false (the default) and handle the async availability.

Can I use @ContentChild to get an element from my own template?

No. Content queries only see projected content from the parent. Use @ViewChild for your own template.

What is the difference between @ViewChild and @ViewChildren?

@ViewChild returns a single reference (the first match), while @ViewChildren returns a QueryList of all matches. Use @ViewChildren when you have multiple elements with the same directive or template reference.

When should I use static: true?

Only when the queried element is always present and does not depend on bindings or structural directives. Using static: true makes the query available in ngOnInit, but it’s rarely needed and can cause errors if used incorrectly.

How do I react to changes in projected content?

Subscribe to the QueryList.changes observable for both @ViewChildren and @ContentChildren. This emits whenever the list of matched elements changes.

Limits and Best Practices

Queries are resolved once per change detection cycle, so they are not reactive to property changes within the queried child—only to the presence or absence of the child itself. For frequent communication, prefer services or state management. Also avoid overusing queries for simple parent-child communication; Angular’s input/output bindings are more explicit and performant for that purpose.

Related

Formula used
in t | Finish time | min | 50 | 0
in d | Distance | km | 10 | 0
out pace | Average pace | min/km | t / d | 2
out speed | Average speed | km/h | d / (t / 60) | 2

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.