JIYIK CN >

Current Location:Home > Learning > WEB FRONT-END > Angular >

Using ngSwitch in Angular

Author:JIYIK Last Updated:2025/04/18 Views:

In common programming languages, you have heard and used switchthe statement at least once.

Statements are used to execute blocks of code when there are many if<pre> statements , we convert these <pre> statements into <pre> statements to save time and make them work more efficiently.switchifswitch

In this tutorial, we will go through an example to create a scenario to execute switchstatements based on the day of the week and display the specific code block for the day of the week.


How to use it in AngularngSwitch

Let us create a new application using the following command.

# angular
ng new my-app

After creating our new application in Angular, we will go to our application directory using this command.

# angular
cd my-app

Now, let's run our application to check if all dependencies are installed correctly.

# angular
ng serve --open

We will create a new component using the following command.

# angular
ng g c greeting

In greeting.component.ts, we will create a Date to store the current day of the week.

# angular
day = new Date().getDay();

We're going to create a switchstatement so that if the date comes back 0it will be Sunday, and 1will be Monday, and so on. So we're going to use [ngSwitch]the binding dayand use to display a different view for each day ngSwitchCasein greeting.component.html.

<h1>Hello! Good Morning</h1>

<div [ngSwitch]="day">
  <div *ngSwitchCase="0">Today is Sunday! Have a nice weekend</div>
  <div *ngSwitchCase="1"><h3>Today is Monday, Have a nice day</h3></div>
  <div *ngSwitchCase="2"><h3>Today is Tuesday, Have a nice day</h3></div>
  <div *ngSwitchCase="3"><h3>Today is Wednesday, Have a nice day</h3></div>
  <div *ngSwitchCase="4"><h3>Today is Thursday, Have a nice day</h3></div>
  <div *ngSwitchCase="5"><h3>Today is Friday, Have a nice day</h3></div>
  <div *ngSwitchCase="6"><h3>Today is Saturday, Have a nice weekend</h3></div>
  <div *ngSwitchDefault>Uh oh! Somethings wrong</div>
</div>

Output:

ngSwitch Angular Example

The above example shows ngSwitchthat shows that the current day is Saturday. So, in this way, using ngSwitchand ngSwitchCase, we can display any code block according to the case scenario.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Enabling HTML5 mode in AngularJS

Publish Date:2025/04/18 Views:148 Category:Angular

This article will guide you through enabling HTML5 mode with deep linking on your AngularJS application. Using HTML5 mode in AngularJS $locationProvider.html5Mode pushState is a way to tell the browser that it needs to use HTML5 mode for UR

Loading spinner in AngularJs

Publish Date:2025/04/18 Views:133 Category:Angular

We will cover how to add a loading spinner while the request is loading and stop the loader while data is loading in AngularJs. Loading spinner in AngularJs Loaders are a part of web applications to make them user-friendly and improve the u

Showing and hiding in Angular

Publish Date:2025/04/18 Views:91 Category:Angular

We will walk through examples of showing and hiding components or elements in Angular. Showing and hiding in Angular While developing business applications we need to hide some data based on user roles or conditions. We have to display the

Downloading files in Angular

Publish Date:2025/04/18 Views:164 Category:Angular

We will look at how to download a file in Angular by clicking a button and show an example. File downloading in Angular Downloading files in Angular is very easy. We can use HTML5 download attributes to download files. # angular a href = "F

Drag and drop in Angular

Publish Date:2025/04/18 Views:187 Category:Angular

We will introduce @angular/cdk/drag-drop the module to accomplish drag and drop in angular. We will also walk through some examples of drag and drop in Angular. Drag and drop in Angular @angular/cdk/drag-drop The module provides you with a

Use TypeScript's getElementById replacement in Angular

Publish Date:2025/04/18 Views:196 Category:Angular

This tutorial guide provides a brief description of replacing AngularJS with TypeScript document.getElementById . This also provides getElementById the best practices for using in Angular with code examples. We will also see the use of DOM

Select an option in the Angular drop-down menu

Publish Date:2025/04/18 Views:112 Category:Angular

We will cover how to set the selected option in an Angular dropdown, set the selected option from an array of options, and set the selected option if the options are dynamic in Angular. Setting the selected option in an Angular dropdown men

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial