Angular ngSwitch Directive – Syntax, Examples & Usage

Introduction

Instead of writing multiple *ngIf statements, Angular provides the ngSwitch directive, which makes your templates cleaner, easier to read, and more maintainable.

The ngSwitch directive works similarly to JavaScript’s switch statement.

Although Angular 17 introduced the new built-in control flow syntax (@switch), and Angular 20+ recommends using @switch for new projects, ngSwitch is still widely used in existing Angular applications.

What is Angular ngSwitch?

ngSwitch is a structural directive that conditionally displays one template from multiple available templates based on the value of an expression.

It consists of three directives:

  • [ngSwitch] – Defines the expression to evaluate.
  • *ngSwitchCase – Specifies a matching case.
  • *ngSwitchDefault – Defines the default template if no cases match.

Unlike *ngIf, which evaluates a single Boolean condition, ngSwitch allows you to handle multiple possible values in a clean and organized way.

Syntax of ngSwitch

Basic syntax:


<div [ngSwitch]="expression">
  <div *ngSwitchCase="'value1'">
    Content 1
  </div>
  <div *ngSwitchCase="'value2'">
    Content 2
  </div>
  <div *ngSwitchDefault>
    Default Content
  </div>
</div>

Angular evaluates the expression and displays the first matching case.

How ngSwitch Works

When Angular processes an ngSwitch block:

📖
ngSwitch Works:
  • It evaluates the expression assigned to [ngSwitch].
  • It compares the result with each *ngSwitchCase.
  • The matching case is rendered.
  • If no case matches, Angular displays *ngSwitchDefault.
  • Non-matching cases are removed from the DOM.

This process integrates with Angular’s change detection system, ensuring the displayed content updates automatically whenever the bound value changes.

Basic Example

Component:


import { Component } from '@angular/core';
@Component({
  selector: 'app-home',
  standalone: true,
  templateUrl: './home.component.html'
})
export class HomeComponent {
  role = 'admin';
}

Template:


<div [ngSwitch]="role">
  <h2 *ngSwitchCase="'admin'">
    Admin Dashboard
  </h2>
  <h2 *ngSwitchCase="'user'">
    User Dashboard
  </h2>
  <h2 *ngSwitchDefault>
    Guest Dashboard
  </h2>
</div>

Output:

Admin Dashboard

Using ngSwitch with Numbers

Component:


status = 2;

Template:


<div [ngSwitch]="status">
  <p *ngSwitchCase="1">
    Pending
  </p>
  <p *ngSwitchCase="2">
    Processing
  </p>
  <p *ngSwitchCase="3">
    Delivered
  </p>
  <p *ngSwitchDefault>
    Unknown Status
  </p>
</div>

Output:

Processing

Using ngSwitch with Strings

Component:


payment = 'UPI';

Template:


<div [ngSwitch]="payment">
  <p *ngSwitchCase="'Credit Card'">
    Pay with Card
  </p>
  <p *ngSwitchCase="'UPI'">
    Pay using UPI
  </p>
  <p *ngSwitchCase="'Cash'">
    Cash Payment
  </p>
  <p *ngSwitchDefault>
    Invalid Payment Method
  </p>
</div>

Angular renders the matching payment option.

Using ngSwitch for User Roles

Component:


role = 'teacher';

Template:


<div [ngSwitch]="role">
  <div *ngSwitchCase="'admin'">
    Admin Panel
  </div>
  <div *ngSwitchCase="'teacher'">
    Teacher Dashboard
  </div>
  <div *ngSwitchCase="'student'">
    Student Dashboard
  </div>
  <div *ngSwitchDefault>
    Guest Page
  </div>
</div>

This is a common real-world use case for role-based interfaces.

Using ngSwitch with Enums

Enums improve readability in larger applications.

Component:


export enum OrderStatus {
  Pending,
  Shipped,
  Delivered
}
status = OrderStatus.Shipped;

Template:


<div [ngSwitch]="status">
  <p *ngSwitchCase="OrderStatus.Pending">
    Pending
  </p>
  <p *ngSwitchCase="OrderStatus.Shipped">
    Shipped
  </p>
  <p *ngSwitchCase="OrderStatus.Delivered">
    Delivered
  </p>
</div>

Using enums avoids magic numbers and improves maintainability.

Using ngSwitchDefault

The default block is displayed when no cases match.

Example:


<div [ngSwitch]="color">
  <p *ngSwitchCase="'red'">
    Red Selected
  </p>
  <p *ngSwitchCase="'blue'">
    Blue Selected
  </p>
  <p *ngSwitchDefault>
    No Color Selected
  </p>
</div>

This is useful for handling unexpected values.

Nested ngSwitch

You can nest switch directives when necessary.

Example:


<div [ngSwitch]="role">
  <div *ngSwitchCase="'admin'">
    <div [ngSwitch]="permission">
      <p *ngSwitchCase="'write'">
        Write Access
      </p>
      <p *ngSwitchDefault>
        Read Only
      </p>
    </div>
  </div>
</div>

However, excessive nesting can reduce readability.

ngSwitch vs *ngIf

*ngIf


<div *ngIf="role === 'admin'">
  Admin
</div>

Ideal for a single condition.

ngSwitch


<div [ngSwitch]="role">
  <div *ngSwitchCase="'admin'">
    Admin
  </div>

  </div>*ngSwitchCase="'user'">
    User
  </div>

</div>

Better suited for multiple possible values.

ngSwitch vs @switch (Angular 20+)

Angular 17 introduced the modern control flow syntax.

ngSwitch


<div [ngSwitch]="status">
<p *ngSwitchCase="'success'">
    Success
  </p>
  <p *ngSwitchDefault>
    Failed
  </p>
</div>

@switch


@switch (status) {
  @case ('success') {
    <p>Success>/p>
  }
  @default {
    <p>Failed>/p>
  }
}

Comparison

Feature ngSwitch @switch
Introduced Early Angular versions Angular 17
Syntax Structural directives Built-in control flow
Readability Good Better
Performance Good Improved
Recommended for New Projects Legacy support Yes (Angular 20+)

Angular recommends using @switch in new applications while ngSwitch remains essential for existing projects.

Best Practices

1. Use ngSwitch for Multiple Conditions

If you’re checking several possible values, ngSwitch is cleaner than multiple *ngIf directives.

2. Keep Cases Simple

Each case should focus on displaying UI rather than performing business logic.

3. Always Include ngSwitchDefault

Providing a default case ensures the application handles unexpected values gracefully.

4. Avoid Deep Nesting

If multiple nested ngSwitch blocks become difficult to manage, consider creating smaller reusable components.

5. Prefer @switch in New Angular Projects

For Angular 20+ applications, use @switch whenever possible because it aligns with Angular’s modern template syntax.

Common Mistakes

1. Forgetting ngSwitchDefault

Without a default block, unexpected values may result in no visible output.

2. Using ngSwitch for Boolean Conditions

For simple true/false conditions, *ngIf or @if is more appropriate.

3. Placing Business Logic in Templates

Avoid complex expressions inside [ngSwitch].

Instead of:


<div [ngSwitch]="calculateStatus()">

Store the value in the component first.

4. Excessive Nesting

Multiple nested switches make templates harder to maintain.

Break large views into reusable components.

Advantages and Disadvantages

Advantages

  • Cleaner than multiple *ngIf directives
  • Easy to read
  • Similar to JavaScript’s switch
  • Organizes multiple conditions effectively
  • Supports a default case
  • Integrates with Angular change detection
  • Ideal for role-based and status-based UIs

Disadvantages

  • Less suitable for Boolean conditions
  • Deep nesting reduces readability
  • @switch is preferred for new Angular 20+ applications
  • Large switch blocks may benefit from component refactoring

For new Angular 20+ projects, consider using the modern @switch syntax for improved readability and consistency with Angular’s latest control flow features.

Conclusion

The Angular ngSwitch directive is a powerful structural directive for rendering one of several possible templates based on the value of an expression.

Although Angular 20+ recommends using the newer @switch control flow syntax for new applications, ngSwitch continues to play an important role in existing Angular projects.

Angular ngSwitch Directive – Objective Questions (MCQs)

Q1. ngSwitch is used for:






Q2. ngSwitch works similar to:






Q3. ngSwitchCase is used for:






Q4. ngSwitchDefault executes when:






Q5. ngSwitch is a:






Related Angular ngSwitch Directive Tutorials