17 April 2020

ngSwitch example in angular


  • Open Command prompt and use below commands to create angular project
  • E:
  • cd E:\TestProjects\Angular
  • ng new ngSwitch-example-in-angular --style=scss --routing --skip-install
  • cd ngSwitch-example-in-angular
  • npm install
  • Open project in visual studio code.
  • Open app.component.ts file and update code as below

  • import { Component } from '@angular/core';

    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.scss']
    })
    export class AppComponent {
      animals = 'Tiger';
    }

  • Open app.component.html file and update code as below


  • <h1>ngSwitch example in angular</h1>

    <ul [ngSwitch]="animals">
      <li *ngSwitchCase="'Tiger'">Tiger</li>
      <li *ngSwitchCase="'Dog'">Dog</li>
      <li *ngSwitchCase="'Cat'">Cat</li>
      <li *ngSwitchCase="'Lion'">Lion</li>
      <li *ngSwitchCase="'Elephant'">Elephant</li>
      <li *ngSwitchDefault>cow</li>
    </ul>


    Output:


    No comments:

    Post a Comment