Angular ngIf & ngSwitch Built-In Directives

In this article, we going to see Angular ngIf & ngSwitch built-in directives. Angular built-in directives provide the most beneficial behavior to the DOM elements. So here you going to see the benefits of ngIf and ngSwitch directives.
Angular ngIf Built-In Directive
Here Angular ngIf directive used to display or remove an element from the DOM. Based on our condition it will display or remove an element, for example, ngIf the condition is true it will display an element or else it will be removed from our DOM.
Here below you have an example and we going to use the same code as we used for ngFor but it lightly modified with the users and their age. Based on age here we going to filter the user’s details with the ngIf condition.
app.component.html
1 2 3 4 5 6 7 |
<ul *ngFor="let user of users"> <li *ngIf="user.age <= 27"> {{ user.name }}, {{ user.age }} </li> </ul> <router-outlet></router-outlet> |
app.component.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'], }) export class AppComponent { users: any[] = [ { "name": "Mohan Raj", "age": 27 }, { "name": "Harish Billa", "age": 28 }, { "name": "Raja Pichandi", "age": 30 }, { "name": "Siva Raj", "age": 27 }, { "name": "Gopi Nath", "age": 30 }, { "name": "Ranjith Kumar", "age": 26 } ]; } |
In the above code, you can see we used ngFor to loop the users list after that, we used ngIf for the condition <=27 if it true this will filter the same & below the age users others will be automatically removed from the DOM.
Output:
Angular ngSwitch Built-In Directive
Angular ngSwitch built-in directive allows you to hide or show the HTML element which you given, based on the expression which matches with given ngSwith expression. The ngSwitch directive works as the as same you have seen in the JavaScript Switch statement.
ngSwitch directive works with the combination of ngSwitchCase and ngSwitchDefault directives to get the result from the various possibilities. By using ngSwitchCase statement which does not match with expression then it goes to another ngSwitchCase statement if still, it does not match with the expression finally the result will show the default element with the ngSwitchDefault.
Example for Angular ngSwitch Directive
Below you have the example for the locations, by using ngSwitch we going to match the expression with the help of ngSwitchCase or else it will display ngSwitchDefault expression location.
app.component.html
1 2 3 4 5 6 7 8 9 |
<ul [ngSwitch]="locations"> <li *ngSwitchCase="'norway'">Norway</li> <li *ngSwitchCase="'cuba'">Cuba</li> <li *ngSwitchCase="'maldives'">Maldives</li> <li *ngSwitchCase="'bali'">Bali</li> <li *ngSwitchDefault>Iceland</li> </ul> <router-outlet></router-outlet> |
app.component.ts
1 2 3 4 5 6 7 8 9 10 |
import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'], }) export class AppComponent { locations = 'norway'; } |
Here now result would be Norway, but the locations don’t meet the location, it will display ngSwitchDefault express Iceland.
Example for Angular ngFor & ngSwitch Directives
In this example, we going to list out achiever’s names and their countries. Using ngFor we going to list out all the person and country with ngSwitch and if the ngSwitch matches with ngSwitchCase country it will display the name of person and country. If nothing matches with the ngSwithcCase it displays ngSwitchDefault expression. In the below code you have the example.
app.component.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<div *ngFor="let person of persons" [ngSwitch]="person.country"> <div *ngSwitchCase="'USA'"> {{ person.name }} ({{ person.country }}) </div> <div *ngSwitchCase="'India'"> {{ person.name }} ({{ person.country }}) </div> <div *ngSwitchCase="'Cuba'"> {{ person.name }} ({{ person.country }}) </div> <div *ngSwitchCase="'UK'"> {{ person.name }} ({{ person.country }}) </div> <div *ngSwitchDefault> {{ person.name }} ({{ person.country }}) </div> </div> <router-outlet></router-outlet> |
app.component.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'], }) export class AppComponent { persons: any[] = [ { "name": "A. P. J. Abdul Kalam", "country": 'India' }, { "name": "Bill Gates", "country": 'USA' }, { "name": "Fidel Castro", "country": 'Cuba' }, { "name": "Steve Jobs", "country": 'USA' }, { "name": "Queen Elizabeth", "country": 'UK' } ]; } |
Output:
Conclusion:
I hope this article will help you to understand the ngIf and ngSwitch directives. If you like this article share it with your friends.
Angular 9Angular CLIngIfngSwitch
Mraj
Creative Designer & Developer specialist by the spirit and a loving blogger by thoughts. If you have any questions let me drop an email with the article name to the following email id: [email protected]