October 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 ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

Angular NG0956: How to Fix DOM Re-Creation in an @for Loop

NG0956 occurs when an Angular @for tracking expression makes every item appear new. Use a stable, unique key such as an item ID when data objects are replaced.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Angular’s NG0956 means an @for loop’s tracking expression made Angular recreate the DOM for every item. If an immutable update replaces each object with a new instance, tracking the object itself can make Angular treat every row as new. Track changing items by a stable, unique property such as todo.id.

What NG0956 means

The track expression tells Angular how to associate collection items with the views rendered in the DOM. As Angular’s @for API documentation explains, that key lets Angular match items across updates and perform the necessary DOM operations when items are added, removed, or moved.

As an Amazon Associate I earn from qualifying purchases.

For that association to work, the key must represent the logical identity of an item, not merely the current JavaScript object instance. If a refresh creates new objects for records that already exist, tracking by object reference can make Angular conclude that every item is different. Angular’s NG0956 documentation describes this as an expensive operation that may also discard DOM-held state.

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

How object tracking triggers the error

Suppose a todo list is rendered with track todo, and an update creates a fresh object for each existing todo:

this.todos = this.todos.map(todo => ({ ...todo, done: true }));

Although each new object represents the same todo, its object reference differs from the old one. With track todo, Angular may treat every row as a new item and rebuild the rendered list, even though only a property binding needed to change.

Fix it with a stable, unique key

Use a property that is unique within the collection and remains unchanged for the lifetime of the logical item. For example, if each todo has a stable, unique id, write:

@for (todo of todos; track todo.id) {
  <li>{{ todo.task }}</li>
}

When the objects are reconstructed, their IDs still identify the same todos, so Angular can preserve the association with the existing views and update bindings instead of rebuilding every row.

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

Choose a tracking key that matches the data

  • Use a stable property, commonly an ID, when the collection can change through refreshes, additions, removals, or reordering. The key should be unique among the items and preserved when the same logical records are represented by new objects.
  • Use $index only for a static collection. Angular describes index tracking as appropriate when the collection’s membership and positions stay fixed. If rows can be inserted, deleted, or reordered, an index can identify a different item after the change; prefer a stable item property instead.
  • Avoid object-reference tracking for reconstructed data. It can be appropriate only when the same object references continue to represent the items across updates. For immutable updates that create replacement objects, a stable property is the safer identity key.

Why DOM recreation matters

Rebuilding every row can do unnecessary work. It can also reset state held by the DOM: Angular specifically notes that focus, text selection, and content loaded in an iframe can be lost when elements are recreated. The Angular error documentation describes the cost qualitatively; it does not quantify a speed impact, so the effect depends on the application and there is no supported benchmark figure to apply generally.

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

If the component still uses *ngFor

The older NgFor directive uses a trackBy function for the corresponding task of associating new object instances with the same logical items. Angular’s NgFor API documentation marks the directive deprecated since Angular v20.0 and recommends using @for. Check your project’s Angular version and migration needs before changing legacy code.

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
Outdated Drivers Are Slowing You DownFree scan - exact matches
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.