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 DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk3 min

How to Use the `@for` Block in Angular Templates

Angular’s @for block renders iterable items, requires a tracking expression, and can show an @empty state. Learn key selection, contextual variables, and migration details.

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.

Use Angular’s @for block to render one template instance for each item in a collection. Every loop needs a track expression: choose a unique, stable item property such as item.id for a changing list, or $index when the list is static. Add @empty after the loop content when you want a message for a collection with no items.

Write a basic @for loop

The general form is @for (item of items; track item.id) { ... }. Angular renders the block for each value in the iterable. For example:

As an Amazon Associate I earn from qualifying purchases.

<ul>
  @for (item of items; track item.id) {
    <li>{{ item.name }}</li>
  }
</ul>

Here, items is the collection, item is the current value, and item.name is rendered for that value. The Angular @for API accepts JavaScript iterables; arrays receive additional performance optimizations.

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

Choose a tracking expression that fits the collection

The required track expression connects each data item with its rendered DOM view. That lets Angular identify which views to keep, update, or move as the collection changes, limiting unnecessary DOM operations. Angular’s control-flow guide recommends a unique property for collections that may change.

Tracking expression Best fit What to consider
track item.id A collection whose membership or order can change Use a property that uniquely identifies each item and remains stable across updates. Duplicate or changing keys do not reliably identify rows.
track $index A collection that is truly static Indexes identify positions, not item identities. If items are inserted, removed, or reordered, a position can refer to a different item.
track item Generally avoid for changing collections Tracking by object reference can make updates significantly slower because Angular cannot map items to DOM nodes as directly.

For example, if a server refreshes a list with new object instances representing the same records, a stable identifier such as item.id allows Angular to associate the records with their existing views. Do not use an identifier that can collide between items or change during the item’s lifetime.

There is a migration behavior worth checking when the tracked property itself changes. If the object reference stays the same, Angular updates bindings, including component inputs, rather than destroying and recreating the element. Angular documents this as a difference from *ngFor in the corresponding case; see the @for API.

Show an empty state with @empty

Place @empty immediately after the loop’s closing brace. Its content is rendered when the collection has no items:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
<ul>
  @for (item of items; track item.id) {
    <li>{{ item.name }}</li>
  } @empty {
    <li>No items are available.</li>
  }
</ul>

This keeps the no-results message alongside the loop that determines whether it should appear. It is useful for empty search results, an initially empty list, or a collection with no available records.

Use the loop’s contextual variables

Inside the block, Angular provides $count, $index, $first, $last, $even, and $odd. Alias a value in the let segment when you want a shorter or clearer name:

@for (item of items; track item.id; let i = $index, last = $last) {
  <li>{{ i }}: {{ item.name }}@if (last) { (last item) }</li>
}

$index is zero-based; $count is the collection’s item count. The first/last and even/odd values describe the current item’s position in the rendered sequence. The Angular control-flow guide documents these contextual values and aliases.

A track expression can use $index, the loop item, and component-class properties or methods. If you alias $index in the let segment, that alias is also available to the tracking expression.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Know the version boundary and migrate from *ngFor

Angular documents built-in control-flow syntax as available from v17. Because @for is built into template syntax, it does not require importing CommonModule for this functionality. The control-flow migration reference provides the migration schematic:

ng generate @angular/core:control-flow

Run it from the Angular workspace to convert eligible control-flow syntax in templates. Review the resulting changes, especially tracking expressions: a key should represent item identity if the collection can change, not merely its current position.

The current NgFor API marks the directive deprecated since Angular v20.0 and recommends @for; removal is intended in a future major release. Applications on Angular v16 or older should follow the version-appropriate Angular control-flow tutorial guidance, which directs those versions to the NgFor documentation. Check the target project’s Angular version and migration guidance before changing templates.

Limitations to account for

  • @for does not support JavaScript break or continue. Filter or prepare the collection before rendering if certain items should be excluded.
  • Every @for requires track; there is no untracked form of the block.
  • Use stable identity for a changing collection. An index-based key is suited to a static collection, not one whose order or membership shifts.

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.

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

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
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair 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.