Add/Remove HTML Elements Using Structural Directives

Introduction

Let us start working with structural directives that let us work on HTML elements by adding/removing its element dynamically. Sometimes, we want a particular set of elements to be visible or render based on some condition and also, we want it to render multiple times in the DOM.

So, the structural directives by Angular provide us with the facility to work with these scenarios. The three common directives are -

  1. ngIf
  2. ngSwitch
  3. ngFor

Prerequisites

Let us create a sample TestApp. For this, you should have installed the below for the development environment.

  1. Node
  2. Npm (comes when you install node)
  3. Angular CLI
  4. Text Editor.

For creating a new application, run the below command on any of your locations.

> ng new TestApp

Create a new component.

> ng g c test

Once your command is completed, you will have a TestApp folder created inside your sample folder.

Now, you will have your project folder called 'TestApp' and your component ‘test’.

Note
See my previous article “Getting started with Angular CLI” if you want to learn the installation and introduction from the basics and start with the execution of the sample application.

ngIf directive

This directive is used to render the particular set of HTML elements on the basis of the condition.

Open test.component.html and add the below content.

  1. <h2>ngIf directive demo!</h2>  
  2. <p *ngIf="true">element - Under *ngIf directive condition.</p>  
Add/Remove HTML Elements Using Structural Directives

Again, modify the contents as below.

  1. <h2>ngIf directive demo!</h2>  
  2. <p *ngIf="false">element - Under *ngIf directive condition.</p>  
Add/Remove HTML Elements Using Structural Directives

Depending on the ngIf condition the element has been rendered above.

We can also perform the class business logic to define the condition for which the element will be rendered accordingly.

Open test.component.ts and add below contents,

  1. import { Component, OnInit } from '@angular/core';  
  2.   
  3. @Component({  
  4.   selector: 'app-test',  
  5.   templateUrl: './test.component.html',  
  6.   styleUrls: ['./test.component.css']  
  7. })  
  8. export class TestComponent implements OnInit {  
  9.   
  10.   public show = true;    
  11.   constructor() { }  
  12.   ngOnInit() {  
  13.   }  
  14. }  

Open test.component.html and add the below contents,

  1. <h2>ngIf directive demo!</h2>  
  2. <p *ngIf="show">element - Under *ngIf directive condition.</p>  

Run the application,

Add/Remove HTML Elements Using Structural Directives 

Open test.component.ts and add change the value to false,

  1. public show = false;  

Save and run the application.

Add/Remove HTML Elements Using Structural Directives 

Else block in *ngIf statement

We can create the Else block that will render when the certain if condition will be true. Let us try with a simple example.

Open test.component.htm and add the below content,

  1. <h2>ngIf directive demo!</h2>  
  2. <p *ngIf="show; else elseStatement">element - Under *ngIf directive condition.</p>  
  3.   
  4. <ng-template #elseStatement>  
  5.     <p>  
  6.         Element - Under Else block.  
  7.     </p>  
  8. </ng-template>  
  9.   
  10. Value of show=false;   

Run the application,

Add/Remove HTML Elements Using Structural Directives 

Ng-template

Ng-template in Angular is used to define the block of the elements that we can use somewhere in HTML. Like above, we have utilized this block in else condition of ngIf.

Let us use the block of the statement in both the conditions instead of using inline statements.

Open test.component.html and add the below contents.

  1. <h2>ngIf directive demo!</h2>  
  2. <p *ngIf="show; then thenStatement; else elseStatement">element - Under *ngIf directive condition.</p>  
  3.   
  4. <ng-template #thenStatement>  
  5.     <p>  
  6.         Element - Under true/if condition.  
  7.     </p>  
  8. </ng-template>  
  9.   
  10. <ng-template #elseStatement>  
  11.     <p>  
  12.         Element - Under false/else condition.  
  13.     </p>  
  14. </ng-template>  

Open test.component.ts and change the value of show=true;

Run the application.

Add/Remove HTML Elements Using Structural Directives 

Open test.component.ts and change the value of show=false;

Run the application.

Add/Remove HTML Elements Using Structural Directives 

ngSwitch

ngSwitch in Angular is similar to other programming switch statements. The only difference is that here we are only rendering the HTML on the basis of the switch statement. It is used when we have multiple sets of value to perform the rendering of a particular HTML statement.

Open test.component.html and add below content.

  1. <h2>ngIf directive demo!</h2>  
  2. <div [ngSwitch]="position">  
  3.     <div *ngSwitchCase="'first'">welcome - you are first.</div>  
  4.     <div *ngSwitchCase="'second'">welcome - you are second.</div>  
  5.     <div *ngSwitchCase="'third'">welcome - you are third.</div>  
  6.     <div *ngSwitchCase="'fourth'">welcome - you are fourth.</div>  
  7. </div>  

Open test.component.ts and add property ‘position’,

public position = “second”

Run the application.

Add/Remove HTML Elements Using Structural Directives 

ngFor directive

The ngFor directive is used in Agular to render the set of elements. It is like the For loop of other programming languages but here it is rendering a list of elements.

Let us take an array of elements and render it through ngFor directives in HTML.

Open test.component.html and add the below contents.

  1. <h2>ngFor directive demo!</h2>  
  2. <div *ngFor="let subject of subjects">  
  3.     <h3>{{subject}}</h3>  
  4. </div>  

Open test.component.ts and add the ‘subjects’ array property.

  1. public subjects = ["English""Maths""Science""Computer""Art"];  

Run the application.

Add/Remove HTML Elements Using Structural Directives 

You can add the array of objects and render its values in the form of a grid and so on.


Similar Articles