Understand ngIf And ngSwitch Directives In Angular 2

Introduction

Angular 2 provides many built-in directives. All these directives provide special behavior to DOM elements. In this article, we will learn ngIf and ngSwitch directives.

ngIf

ngIf directive recreates or removes the portions of the DOM which are based on the condition. Here, the condition is in the form of expression. The expression assigned to directive is set to true. Angular recreates the template and it is rendered in DOM, else it is removed from the DOM.

Syntax

<ANY ELEMENT *ngIf="condition">

</ ANY ELEMENT >


OR

< ANY ELEMENT template="ngIf condition">

</ ANY ELEMENT >


OR

<template [ngIf]="condition">
< ANY ELEMENT >...</ ANY ELEMENT >
</template>


Example

In the example mentioned below, I had shown or hidden some text, which is based on checkbox checked value.

App.componet.ts

  1. import { Component } from '@angular/core';  
  2. @Component({  
  3.   selector: 'test-app',  
  4.   templateUrl: './app/directiveExample.html'  
  5. })  
  6. export class AppComponent {   
  7.     show = false;  
  8. }

directiveExample.html

  1. <h4>*ngIf in Angular 2 Application</h4>  
  2. <div>  
  3.     <h4>*ngIf Example</h4>  
  4.      Show Text: <input type="checkbox" [(ngModel)]="show" />    
  5.     <div *ngIf="show">    
  6.         This is my ng-If directive test.    
  7.     </div>  
  8. </div>

Output



ngSwitch

The ngSwitch directive uses switch DOM structure conditionally in our HTML template, which is based on an expression. The ngSwitchCase and ngSwitchDefault directives are used to show and hide the template within ngSwitch directive. The "Case" attribute is used to inform the ngSwitch directive which template is to display based on an expression.

The ngSwitchCase directive creates the sub tree when the expression evaluates to the same value as the switching expression. If the multiple matches are found, all matches are displayed. The ngSwitchDefault creates a sub tree when no case expression evaluates to the same value as the switching expression.

Example

In the example mentioned below, based on dropdown selection, the text value has been changed.

App.component.ts

  1. import { Component } from '@angular/core';  
  2. @Component({  
  3.   selector: 'test-app',  
  4.   templateUrl: './app/directiveExample.html'  
  5. })  
  6. export class AppComponent {   
  7.   data = ['<<Please Select>>''Tejas''Jignesh''Rakesh'];  
  8.   selected= "";  
  9. }

directiveExample.html

  1. <div>  
  2.     <h4>*ngSwitch Example</h4>  
  3.     <select [(ngModel)]="selected">  
  4.         <option *ngFor="#item of data" [value]="item">{{item}}</option>  
  5.     </select>  
  6.     <div [ngSwitch]="selected">    
  7.         <div *ngSwitchCase="'Tejas'">You have select "Tejas"</div>    
  8.         <div *ngSwitchCase="'Rakesh'">You have select "Rakesh"</div>    
  9.         <div *ngSwitchCase="'Jignesh'">You have select "Jignesh"</div>    
  10.         <div *ngSwitchDefault>Please select name</div>  
  11.     </div>   
  12. </div> 

Output



Conclusion

This article will help you to learn ngIf and ngSwitch directive in Angular 2. These directives are helpful, as it allows us to show/hide templates based on an expression.

Note

Here, I have not included the dependency of the example project. To run the project, you need to perform "npm install" command to download the project dependency.