Tevpro insights

Angular @for and track: Syntax, Stable Keys, and *ngFor Migration

Use Angular @for with a stable track expression. See working examples and the difference from legacy *ngFor trackBy.

Angular

Key takeaways

  • Use @for (item of items; track item.id) with a stable unique key.
  • The legacy *ngFor directive uses a trackBy callback; it is not @for syntax.
  • Reserve track $index for static collections that never reorder or change.

Angular @for repeats a template for each item in a collection. Use track item.id when each item has a stable unique ID. The older *ngFor directive uses trackBy: trackById instead; that syntax does not belong inside @for.

The difference matters when a list is reordered, filtered, or refreshed. Angular uses the tracking value to connect an item to its existing view and limit unnecessary DOM work.

If your team is modernizing Angular applications or rebuilding legacy web apps, Tevpro's custom software development services help companies ship faster without carrying old frontend baggage.

A working @for example

Given an array of users with stable IDs, write the loop as a block. There is no *for attribute and no colon after track.

Component data
typescript
type User = { id: number; name: string };
users: User[] = [
  { id: 1, name: 'Justin' },
  { id: 2, name: 'Keith' },
  { id: 3, name: 'Eric' },
];
Template
angular-html
@for (user of users; track user.id) {
  <p>{{ user.name }}</p>
} @empty {
  <p>No users found.</p>
}

Choose a tracking key that matches the data

Use a unique ID or UUID when items can be added, removed, or reordered. For a truly static collection that never changes, track $index is an option. Tracking the item reference itself is a last resort because replacing objects can force more DOM work.

Static collection only
angular-html
@for (label of labels; track $index) {
  <span>{{ label }}</span>
}

How is *ngFor trackBy different?

Older Angular templates use a structural directive with a callback. The function receives an index and an item and returns a stable key. That callback is an *ngFor pattern, not an @for clause.

Legacy component callback
typescript
trackById(_index: number, user: User): number {
  return user.id;
}
Legacy *ngFor template
angular-html
<p *ngFor="let user of users; trackBy: trackById">
  {{ user.name }}
</p>

For a new template, prefer @for (user of users; track user.id). Angular now marks NgFor deprecated and recommends the @for block. If you are migrating an existing template, check that the chosen key stays stable after sorting, filtering, and replacing records.

Common mistakes to avoid

Do not write <element *for="...">, track: item.id, or trackBy: callback inside an @for block. Do not choose $index for a list whose items move, because a position is not an item identity. A track expression should identify the same logical record on the next render.

The short rule: @for uses a track expression; *ngFor uses trackBy. Keep the two syntaxes separate and test list changes, not just the first render.

Sources

  1. [1] Angular template control flowangular.dev
  2. [2] Angular NgFor API and deprecationangular.dev

Written by Kim Pham

Why work with us

Why Tevpro?

Whether you’re a startup with a bold product idea or an established company seeking a stronger delivery partner, Tevpro delivers results. Our expert consultants specialize in building secure, scalable applications that simplify operations and drive real ROI.