Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversHalloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk3 min

Angular Structural Directives: How the * Syntax Works, When to Write Your Own, and What Replaces NgIf and NgFor

The * in Angular structural directives is shorthand for an ng-template wrapper. Learn how it expands, when to write a custom directive, and how to migrate from deprecated NgIf and NgFor.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A structural directive in Angular is a directive applied to an <ng-template> that decides whether that template’s content renders, or how many times. The * prefix you see on *select is shorthand for that wrapper. For everyday conditions and loops, Angular now recommends the built-in @if, @for and @switch blocks. NgIf and NgFor are deprecated. This guide covers the mechanics, the decision between built-in blocks and a custom directive, and the migration path.

What a structural directive is

Angular’s structural directives guide says: “Structural directives are directives applied to an <ng-template> element that conditionally or repeatedly render the content of that <ng-template>.” An <ng-template> renders nothing by itself. The directive decides whether to create a view from it, and how many.

As an Amazon Associate I earn from qualifying purchases.

How the * shorthand maps to ng-template

When you write *select on an element, Angular creates an <ng-template> to host the directive and wraps the annotated element and its descendants inside it. The asterisk is not a special element. It is syntax sugar for the wrapper.

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.

Microsyntax expansion

The string after the * packs directive inputs and template variables into a single attribute. Per the guide, *myDir="let item of items; index as i" expands into an <ng-template> that has:

  • the directive itself;
  • a let-item context variable;
  • a myDirOf input bound to items;
  • a variable i exported from the template context as index.

The key point is that the directive selector prefixes each key expression. The key of becomes the input myDirOf, so your directive must declare an input with exactly that name to receive it.

One structural directive per element

The shorthand corresponds to one <ng-template>, so an element can carry only one * directive. To combine behaviors, nest elements or add a grouping layer with <ng-container>, which adds no extra DOM element. See the ng-container guide.

Built-in control flow or a custom directive?

For routine conditions, loops and switching, use @if, @for and @switch. Write a custom structural directive when you need a reusable rendering rule those blocks don’t cover. The guide’s examples are gating content behind a permission check, and rendering a template once data from an external source arrives.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Question Leans toward
Is it an ordinary condition, iteration or switch? Built-in @if / @for / @switch
Is it a domain-specific rule reused across many templates (permissions, async availability)? Custom structural directive
Do you need to supply your own template context variables? Custom directive
Do you need stronger template type narrowing than the built-ins give? Custom directive with a template guard
Are you replacing a deprecated NgIf or NgFor? Migrate to built-in blocks

Building a custom structural directive

Two APIs do the work:

  • TemplateRef gives the directive access to the template it is attached to.
  • ViewContainerRef represents the spot where the directive can create embedded views from that template.

The directive injects both, then creates or clears an embedded view when its input changes. Angular’s template type checker can also use template guards, which narrow input types and validate the template context, so templates using your directive get accurate type errors. Details and a full example are in the official guide.

NgIf and NgFor: deprecation status

The current API references mark both as deprecated since v20.0, pointing to @if and @for as replacements. The removal timelines differ, so don’t conflate them:

Directive Status Removal intent
NgIf Deprecated since v20.0; use @if A future major release, not named
NgFor Deprecated since v20.0; use @for v22 (per the v20 API page)

These details are version-sensitive, so check the linked API pages for your Angular version.

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

Migrating to built-in control flow

Built-in control flow has been available since v17. It is part of the template syntax, so it needs no CommonModule import. Angular provides a schematic to convert existing templates:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
ng generate @angular/core:control-flow

It can target a specific workspace path and can be configured to preserve formatting. See the control flow migration guide for the exact options. Review the diff afterward and run your tests, as with any automated code change.

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
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

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.