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

How to Migrate from NgClass to Class Bindings in Angular

Use Angular’s built-in class bindings for simple conditional classes, but retain NgClass when you need a Set or multi-class object keys. Here’s how to migrate safely.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For simple conditional classes, replace NgClass with Angular’s built-in class bindings: use [class.active]="isActive" for one class, or [class]="{ active: isActive, dense: isDense }" for several. Angular recommends built-in bindings for simpler syntax and notes that NgClass has an additional performance cost. Keep NgClass when you depend on behavior the built-in bindings do not support, such as a Set input or a space-separated class list in one object key.

Choose the binding that fits the classes

Toggle one class

Use an individual class binding when a single class depends on a condition:

As an Amazon Associate I earn from qualifying purchases.

<div [class.active]="isActive">Content</div>

Angular applies active when the expression is truthy and removes it when the expression is false.

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

Bind several classes

Use [class] with an object when each class has its own condition:

<div [class]="{ admin: isAdmin, dense: isDense }">Content</div>

You can also bind a space-separated string or an array of class strings. For example, an existing simple object binding can be changed directly:

<!-- Before -->
<div [ngClass]="{admin: isAdmin, dense: density === 'high'}"></div>

<!-- After -->
<div [class]="{admin: isAdmin, dense: density === 'high'}"></div>

What changes when you replace NgClass?

Both approaches add and remove classes, but their accepted values and edge cases are not identical.

Capability NgClass Built-in class bindings
One conditional class Can be expressed with a class map [class.name]="condition"
Multiple classes String, string array, Set<string>, or object [class] accepts a space-separated string, array, or object
Object key containing multiple class names Supported Not supported
Object or array update detection Directive behavior applies Angular documents reference comparison; replace the value rather than mutating it in place
Directive dependency Uses the NgClass directive Uses template binding syntax

Angular’s style guide recommends built-in class bindings for their simpler syntax and states that NgClass and NgStyle incur an additional performance cost. The documentation does not quantify a speedup, so treat this as a qualitative reason to prefer the built-in syntax—not as a promised benchmark result. See the Angular style guide.

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

Handle object and array updates correctly

For [class] bound to an array or object, Angular checks whether the new value has a different reference from the previous value. If you change a property on the same object or edit the same array in place, the reference stays unchanged and the documented class-binding update is not triggered. Create and assign a replacement value when the classes change.

// Replace the object rather than mutating it in place
this.classes = { ...this.classes, active: true };

Static classes, a direct class binding, and individual class bindings can coexist on an element. Angular combines them in the rendered output, but does not guarantee their order. Avoid tests or application logic that depend on a particular order in the class attribute. Details are in Angular’s template binding guide.

Know when NgClass is still the better fit

Keep NgClass if the existing expression uses a capability that does not carry over directly:

  • A Set<string> value: NgClass accepts a set. Angular’s documented [class] forms are strings, arrays, and objects, so do not pass a set through unchanged.
  • Several class names in one object key: NgClass supports a key such as 'class1 class2'. Built-in class bindings do not support space-separated class names in one object key.

You may still convert these cases deliberately—for example, by creating individual bindings—but the result should preserve the original conditions and behavior.

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

Run Angular’s migration schematic

Angular provides a schematic to convert usages it considers safe. From the project root, run:

ng generate @angular/core:ngclass-to-class

By default, the migration avoids object literals with keys containing multiple space-separated classes. To ask it to convert those cases into one individual binding per class, use the documented option:

ng generate @angular/core:ngclass-to-class --migrate-space-separated-key

For a key like 'class1 class2', that option generates bindings equivalent to [class.class1]="condition" and [class.class2]="condition". The conversion is not universal: Angular documents that the schematic migrates only usages it considers safe.

  1. Run the command from the Angular workspace root.
  2. Review the generated diff, especially any skipped usages or converted multi-class keys.
  3. Run the project’s normal checks and verify the affected UI states in the application.

See Angular’s NgClass-to-class migration guide and migrations overview for the current schematic details.

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

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.