Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk4 min

How to Reference Component Children with Queries in Angular

Use view queries for a component’s own template and content queries for projected content. Learn how singular, plural, optional, required, and decorator-based queries differ.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use viewChild or viewChildren to find components, directives, or other targets in your component’s own template. Use contentChild or contentChildren to find content projected into your component. Choose the singular or plural form according to whether you expect one match or several.

For new code, Angular recommends signal-based query functions; the decorator-based query APIs remain supported. The examples below follow Angular’s current guide, which does not specify a minimum framework version. Check the API documentation for the Angular version installed in your project before applying version-specific changes. Angular’s queries guide

As an Amazon Associate I earn from qualifying purchases.

Choose a query by where the child is declared

The key distinction is template ownership. A component’s view is the template it declares itself. Its content is the nested content supplied by the component’s consumer where the component is used.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Where the target appears One match Multiple matches
In the querying component’s own template viewChild viewChildren
In content projected into the querying component contentChild contentChildren

These queries do not cross component boundaries. A query can find targets within the relevant template, but it cannot inspect a separate component’s internal template.

Query the component’s own template

Use a view query when the target is declared in the querying component’s template. The locator may be a component or directive type, a template reference variable, or a provider token.

Find one child

A signal-based viewChild query returns a signal. Call it to read the current result. If the target may not exist—for example, because it is conditionally rendered—the result can be undefined.

header = viewChild(CustomCardHeader);
headerText = computed(() => this.header()?.text);

Optional chaining lets the computed value handle a missing child. You can also use an explicit conditional branch when the absence needs separate handling.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Find several children

Use viewChildren when the template can contain multiple matching targets. Its signal returns the current collection; call the signal to read it.

Query projected content

Use content queries for targets nested inside a component’s projected content. For example, a component that accepts consumer-provided content can query that content rather than its own template.

Find one projected target

contentChild returns one result and traverses descendants by default, within the same template. As with a single view query, the result can be absent; use the required form only if a match is an invariant of the component’s usage.

Find multiple projected targets

contentChildren returns a collection, but by default it finds direct children only. To include deeper descendants within the same template, pass { descendants: true }.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
items = contentChildren(MenuItem, { descendants: true });

Setting descendants: true does not make the query cross into another component’s template.

Decide whether a match is optional or required

Signal-based single-result queries can be absent, and Angular updates query results as application state changes. A target that appears or disappears because of conditional rendering can therefore change the result over time.

  • Use the ordinary single-result query when absence is possible, and handle undefined in code that reads it.
  • Use viewChild.required or contentChild.required when the component’s contract guarantees a match. The required form gives a non-optional result type; Angular reports an error if there is no match.

Do not mark a query required merely to avoid handling an optional child. The required form is appropriate when a missing target really indicates an invalid component configuration.

Choose a locator and, if needed, a different read token

A query locator can be a component or directive type, a template reference variable name as a string, or a provider token. CSS selectors are not supported as query locators.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The read option lets a query return a different value available from the matched element’s injector. Depending on what the element provides, examples include ElementRef, TemplateRef, and Injector. Use this when you need one of those values rather than the value identified by the locator.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Use decorator queries in existing code

@ViewChild, @ViewChildren, @ContentChild, and @ContentChildren remain supported. Angular recommends signal-based query functions for new projects, but existing decorator-based code does not need to be rewritten solely because the newer API exists. Angular’s query API guidance

Lifecycle timing for single-result decorators

With the default dynamic behavior, decorator query results are commonly read after the corresponding view or content has initialized. Use static: true with @ViewChild or @ContentChild only when the target is guaranteed to be present and does not depend on conditional rendering. A static result is available in ngOnInit, but it does not update after initialization.

Collections from plural decorators

@ViewChildren and @ContentChildren expose a QueryList. It provides array-like helpers and a changes observable for observing changes to the collection.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A quick choice checklist

  • Target declared in your template: choose a view query.
  • Target supplied as projected content: choose a content query.
  • One expected match: choose the singular form; several: choose the plural form.
  • Projected descendants beyond direct children: use contentChildren with { descendants: true } when needed.
  • Target may be absent: handle the optional single-result value; use .required only for a guaranteed match.
  • New implementation: prefer signal queries; existing decorator code remains supported.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.