Understanding With ng-Switch Statements In Angular

Ng-Switch statements in Angular

 
Ng switch is actually comprised of two directives, an attribute directive and a structural directive. It's very similar to switch statements in Javascript andnother programming languages but only in the template.
 
The ng-switch lets you hide and show HTML elements. Depending on the expression we can also define a default section by using the ng switch default directives to show a section if none of the other sections gets a match.
 
There are three things to keep in mind: ng switch, ng-switch-case, ng-switch.
 
Ng switch statements are where one alternative is chosen from the number of alternatives. And ng switch is performed indirectly. That means users have the choice to choose anyone from the list. There have no bound to choose any number or element.
 
Example of ng-switch statements - list of years, list of the month, calendar, weak days, and student data.
 
Example of switch statements
 
First, you have to create an application using the command “ng new application name”.
 
Then you have to open this application and then create a component using the command “ ng g component component-name”.
 
Now go to the module file and check all dependencies and register our component in the bootstrap section.
 
Then you have to go to the .html page and take a drop-down.
 
Then create a function into the dropdown and then create an event into the function.
 
And then take no of alternatives according to your requirements.
 
Now you have to take a <div></div> using div tag.
 
Then you have to take an array [ switch ]=” choose”. And then add a variable for the array with any name.
 
Then take the switchCase into the <h3></h3> tag.
 
In the end, take a default case for the default value.
 
Now you have to go to the .ts file and take your variable publicly.
 
Then you have to create a function you have to create in the HTML file. Then take any variable and create a query.
 
switch.html
  1. <h2><p> NG Switch Statements</p></h2>    
  2. <select (change)="setvalue($event)">    
  3.     <option value="">...Select...</option>    
  4.     <option value="one  ">one</option>    
  5.     <option value="Monday ">Monday</option>    
  6.     <option value="Tuesday ">Tuesday</option>    
  7.     <option value="Wednesday ">Wednesday</option>    
  8.     <option value="Thrusday ">Thrusday</option>    
  9.     <option value="Friday ">Friday</option>    
  10.     <option value="Saturday ">Saturday</option>    
  11.     <option value="another ">Another</option>    
  12.     
  13. </select>    
  14.     
  15. <div [ngSwitch]="choose">    
  16.     <h3 *ngSwitchCase="'one'"></h3>    
  17.     <h3 *ngSwitchCase="'Monday'"></h3>    
  18.     <h3 *ngSwitchCase="'Tuesday'"></h3>    
  19.     <h3 *ngSwitchCase="'Wednesday'"></h3>    
  20.     <h3 *ngSwitchCase="'Thrusday'"></h3>    
  21.     <h3 *ngSwitchCase="'Friday'"></h3>    
  22.     <h3 *ngSwitchCase="'saturday'"></h3>    
  23. </div>     
switch.ts
  1. import { Component, OnInit } from '@angular/core';    
  2. @Component({    
  3.   selector: 'app-switch',    
  4.   templateUrl: './switch.component.html',    
  5.   styleUrls: ['./switch.component.css']    
  6. })    
  7. export class SwitchComponent{    
  8.  public choose='';    
  9.  setvalue(drp:any){    
  10.    this.choose=drp.target.value;    
  11.  }    
  12. }     
module.ts
  1. import { NgModule } from '@angular/core';    
  2. import { BrowserModule } from '@angular/platform-browser';    
  3. import { AppRoutingModule } from './app-routing.module';    
  4. import {HttpClientModule } from '@angular/common/http'    
  5. import { AppComponent } from './app.component';    
  6. import { AddComponent } from './add/add.component';    
  7. import { RouterModule, Routes} from '@angular/router';    
  8. import{BookService } from './book.service';    
  9. import {InMemoryWebApiModule } from 'angular-in-memory-web-api';    
  10. import {TestData } from './testdata'    
  11. import { FormsModule, ReactiveFormsModule } from '@angular/forms';    
  12. import { ShowdataComponent } from './showdata/showdata.component';    
  13. import { CommonModule } from '@angular/common';    
  14. import { IfelseComponent } from './ifelse/ifelse.component';    
  15. import { SwitchComponent } from './switch/switch.component';    
  16. const routes: Routes = [    
  17.   {path:'', component:AddComponent},    
  18.   { path:'add', component:AddComponent}    
  19. ];    
  20. @NgModule({    
  21.   declarations: [    
  22.     AppComponent,    
  23.     AddComponent,    
  24.     ShowdataComponent,    
  25.     IfelseComponent,    
  26.     SwitchComponent    
  27.   ],    
  28.   imports: [    
  29.     BrowserModule,    
  30.     AppRoutingModule,RouterModule.forRoot(routes),ReactiveFormsModule, FormsModule,    
  31.     HttpClientModule, CommonModule,    
  32.     InMemoryWebApiModule.forRoot(TestData)    
  33.   ],    
  34.   providers: [BookService],    
  35.   bootstrap: [SwitchComponent]    
  36. })    
  37. export class AppModule { }     
main.ts
  1. import { enableProdMode } from '@angular/core';    
  2. import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';    
  3. import { AppModule } from './app/app.module';    
  4. import { environment } from './environments/environment';    
  5. if (environment.production) {    
  6.   enableProdMode();    
  7. }    
  8. platformBrowserDynamic().bootstrapModule(AppModule)    
  9.   .catch(err => console.error(err));    
index.html
  1. <!doctype html>    
  2. <html lang="en">    
  3. <head>    
  4.   <meta charset="utf-8">    
  5.   <title>Dhwani</title>    
  6.   <base href="/">    
  7.   <meta name="viewport" content="width=device-width, initial-scale=1">    
  8.   <link rel="icon" type="image/x-icon" href="favicon.ico">    
  9. </head>    
  10. <body>    
  11.   <app-switch></app-switch>    
  12. </body>    
  13. </html>     
Now save all the files and compile the project using the command “ng serve”.
 
After successfully completing the project you have to go to the browser and hit “localhost:4200”. After a few seconds your browser will show the output like,
 
Understanding With ng-Switch Statements In Angular
 
Now you can see that there is no selective value in the dropdown and the value will be shown by default.
 
Now select any value from the dropdown and after selecting the value the output will be shown,
 
Understanding With ng-Switch Statements In Angular
 
Now you can see that Monday will be selected from the dropdown, and Monday will be shown on the display.
 
Now select Friday from the dropdown and see the output.
 
Understanding With ng-Switch Statements In Angular
 
Now you can see that Friday will be selected from the dropdown and the name Friday will be shown in the place of the default.
 
Now you can see that our switch statement application performed its work properly. 
 
Follow Chaman Gautam to learn more about angular. Follow C# Corner to learn about more technology.