AngularJS 2.0 From The Beginning - Structural Directives - Day Seven

I am here to continue the discussion around AngularJS 2.0. Today, we will discuss Structural Directives in AngularJS 2. Also, in case, you did not have a look at the previous articles of this series, go through the links, mentioned below.

In my previous article, I already discussed about the attribute directives in Angular 2.0. In that article, we discussed about some inbuilt attribute directives and also discussed about how to create custom attribute directives. Now in this article, I will discuss about another part of the directives -- structural directives. Components are simply directives with templates and they are the most commonly used in our application. Attribute directives are basically used to change the appearance of an element in our application, whereas structural directives can be used to modify the DOM by adding or removing any DOM elements and for this reason, it can be used in different scenarios.

In Angular 2.0, there are some in built structural directives i.e. *ngIf, *ngFor, *ngSwitch etc. Normally structural directives are prefixed with an asterisk which signals to Angular that the structure of the DOM element within the directive may change on depend of the some conditions.

ngIf

In Angular  version 1.x, there was the ng-show and ng-hide directives which would show or hide the DOM elements on what the given expression evaluates by setting the display CSS property. In Angular 2.0, these two directives have been removed from the framework and  a new directive named ngIf was introduced. The main difference of ngIf directive over ng-show or ng-hide is that it actually removes the element or components entirely from DOM. For ng-show or ng-hide, angular keeps the DOM elements/components in the page, so any component behaviors may keep running even if the component is not visible in the page. In Angular 2.0, ng-show or ng-hide directive is not available but we can obtain the same functionality by using the [style.display] property of any element. Now, one question always arises in our mind which is why angular removes component or elements from DOM in the case of ngIf directives? Actually, although in the earlier version, it hides the component or element, still the elements or components are attached with DOM. So it continues to fire its event listener. Also it keeps changing while the model data has been changed due to model binding. So in this way, this invisible components or elements use resources which might be useful for somewhere else. The performance and memory burden can be substantial and the user may not benefit at all. Setting ngIf value to false does effect the component resource consumptions. Angular removes the element from DOM, stops the change detection for the associated component, detaches it from DOM events, and destroys the components. The component can be garbage collected to free up memory. Components often have child components which themselves have children. All of them have been destroyed when ngIf destroys the common ancestor.

For demonstrating the ngIf, do the following steps,
 
Step 1

First Create the app.module.ts file and add the below code,
  1. import { NgModule } from '@angular/core';  
  2. import { FormsModule  } from '@angular/forms';  
  3. import { BrowserModule } from '@angular/platform-browser';  
  4.   
  5. import { ParentComponent } from './src/app.component.parent';  
  6. import { NgIfComponent } from './src/app.component.ngIf';  
  7.   
  8. @NgModule({  
  9.     imports: [BrowserModule, FormsModule],  
  10.     declarations: [ParentComponent, NgIfComponent],  
  11.     bootstrap: [ParentComponent]  
  12. })  
  13. export class AppModule { } 
Step 2

Now create another ts file named app.component.parent.ts and add the below code,
  1. import { Component, OnInit } from '@angular/core';  
  2.   
  3. @Component({  
  4.     moduleId: module.id,  
  5.     selector: 'structural-directive',  
  6.     templateUrl: 'app.component.parent.html',  
  7. })  
  8.   
  9. export class ParentComponent implements OnInit {  
  10.     constructor() { }  
  11.   
  12.     ngOnInit() { }  

Step 3

Now create a html file named app.component.parent.html and add the below code,
  1. <div>  
  2.     <h2 class="badge">Structural Directives Demonstrate</h2>  
  3.     <br />  
  4.     <toggle-text></toggle-text>  
  5. </div> 
Step 4

Now create another ts file named app.component.ngIf.ts and add the below code,
  1. import { Component, OnInit, Input } from '@angular/core';  
  2.   
  3. @Component({  
  4.     moduleId: module.id,  
  5.     selector: 'toggle-text',  
  6.     templateUrl: 'app.component.ngIf.html'  
  7. })  
  8.   
  9. export class NgIfComponent implements OnInit {  
  10.     showInfo: boolean = false;  
  11.     caption: string = 'Show Text';  
  12.   
  13.     constructor() { }  
  14.     ngOnInit() { }  
  15.   
  16.     changeData(): void {  
  17.         this.showInfo = !this.showInfo;  
  18.         if (this.showInfo) {  
  19.             this.caption = 'Hide Text';  
  20.         }  
  21.         else {  
  22.             this.caption = 'Show Text';  
  23.         }  
  24.     }  

Step 5

Now create another html file named app.component.ngif.html and add the below code,
  1. <div>  
  2.     <input type="button" value="{{caption}}" (click)="changeData()"/>  
  3.     <br />  
  4.     <h2 *ngIf="showInfo"><span>Demonstrate of Structural Directives - *ngIf</span></h2>  
  5. </div> 
Step 6

Now create another html file named index.html and add the below code,
  1. <!DOCTYPE html>  
  2. <html>  
  3.   <head>  
  4.     <title>Angular2 - Structural Directivestitle>  
  5.     <meta charset="UTF-8">  
  6.     <meta name="viewport" content="width=device-width, initial-scale=1">  
  7.     <link rel="stylesheet" href="../styles.css">  
  8.   
  9.     <!-- Polyfill(s) for older browsers -->  
  10.     <script src="../node_modules/core-js/client/shim.min.js"></script>  
  11.     <script src="../node_modules/zone.js/dist/zone.js"></script>  
  12.     <script src="../node_modules/reflect-metadata/Reflect.js"></script>  
  13.     <script src="../node_modules/systemjs/dist/system.src.js"></script>  
  14.   
  15.     <script src="../systemjs.config.js"></script>  
  16.     <script>  
  17.       System.import('app').catch(function(err){ console.error(err); });  
  18.     </script>  
  19.   </head>  
  20.   
  21.   <body>  
  22.     <structural-directive>Loading</structural-directive>  
  23.   </body>  
  24. </html> 
Now run the code and the output as below,


  
ngFor

The ngFor directives instantiates a template once per item from an iterable. The context of each instantiated template inherits from the outer context with the given loop variable. ngFor provides several exported values that can be used to local variables :-

  • index will be set to the current loop iteration for each template context
  • first will be set to a boolean value indicating whether the item is the first one in the iteration.
  • last will be set to a boolean value indicating whether the item is the last one in the iteration.
  • even will be set to a boolean value indicating whether this item has an even index.
  • odd will be set to a boolean value indicating whether this item has an odd index

Angular uses object identity to track insertions and deletions within the iterator and reproduce those changes in the DOM. This has important implications for animations and any stateful controls (such as <input> elements which accept user input) that are present. Inserted rows can be animated in, deleted rows can be animated out, and unchanged rows retain any unsaved state such as user input.

For demonstrate the ngFor directive, do the following code,

Step 1

Add a ts file named app.component.ngFor.ts and add the below code,
  1. import { Component, OnInit, Directive } from '@angular/core';  
  2.   
  3. @Component({  
  4.     moduleId: module.id,  
  5.     selector: 'product-list',  
  6.     templateUrl: 'app.component.ngFor.html'  
  7. })  
  8.       
  9. export class NgForComponent implements OnInit {  
  10.     productList: Array<string> = ['IPhone','Galaxy 7.0','Blackberry 10Z'];  
  11.   
  12.     constructor() { }  
  13.     ngOnInit() { }  

Step 2

Add a html file named app.component.ngFor.html and add the below code,
  1. <div>  
  2.     <h2>Demonstrate ngFor</h2>  
  3.     <ul>  
  4.         <li *ngFor="let item of productList">  
  5.             {{item}}  
  6.         </li>  
  7.     </ul>  
  8. </div> 
Now add the product-list selector tag within app.component.parent.html file and also take the reference of ngForComponent within the app.module.ts file and then run the code,
 
ngSwitch

The ngSwitch directives is actually made of two directives, an attribute directive and a structural directive. It is similar to switch statement in javascript or other languages. ngSwitch stamps our nested views when their match expression value matches the value of the switch expression. The expression bound to the directives defines what will compared against in the switch structural directives. If an expression bound to ngSwitchCase matches the one given to ngSwitch, those components are created and the others destroyed. If none of the cases match, then components that have ngSwitchDefault bound to them will be created and the others destroyed. Note that multiple components can be matched using ngSwitchCase and in those cases all matching components will be created. Since components are created or destroyed be aware of the costs in doing so.

For demonstrating this, do the following,

Step 1

Add a ts file named app.component.ngSwitch.ts and add the below code,
  1. import { Component, OnInit, Directive } from '@angular/core';  
  2.   
  3. @Component({  
  4.     moduleId: module.id,  
  5.     selector: 'student-list',  
  6.     templateUrl: 'app.component.ngSwitch.html'  
  7. })  
  8.   
  9. export class NgSwitchComponent implements OnInit {  
  10.     studentList: Array<any> = new Array<any>();  
  11.   
  12.     constructor() { }  
  13.     ngOnInit() {  
  14.         this.studentList = [  
  15.             { SrlNo: 1, Name: 'Rajib Basak', Course: 'Bsc(Hons)', Grade: 'A' },  
  16.             { SrlNo: 2, Name: 'Rajib Basak1', Course: 'BA', Grade: 'B' },  
  17.             { SrlNo: 3, Name: 'Rajib Basak2', Course: 'BCom', Grade: 'A' },  
  18.             { SrlNo: 4, Name: 'Rajib Basak3', Course: 'Bsc-Hons', Grade: 'C' },  
  19.             { SrlNo: 5, Name: 'Rajib Basak4', Course: 'MBA', Grade: 'B' },  
  20.             { SrlNo: 6, Name: 'Rajib Basak5', Course: 'MSc', Grade: 'B' },  
  21.             { SrlNo: 7, Name: 'Rajib Basak6', Course: 'MBA', Grade: 'A' },  
  22.             { SrlNo: 8, Name: 'Rajib Basak7', Course: 'MSc.', Grade: 'C' },  
  23.             { SrlNo: 9, Name: 'Rajib Basak8', Course: 'MA', Grade: 'D' },  
  24.             { SrlNo: 10, Name: 'Rajib Basak9', Course: 'B.Tech', Grade: 'A' }  
  25.         ];  
  26.     }  

Step 2

Add a html file named app.component.ngSwitch.html and add the below code, 
  1. <div>  
  2.     <h2>Demonstrate ngSwitch</h2>  
  3.     <table style="width:100%;border:solid;border-color:blue;border-width:thin;">  
  4.         <thead>  
  5.             <tr >  
  6.                 <td>Srl No</td>  
  7.                 <td>Student Name</td>  
  8.                 <td>Course</td>  
  9.                 <td>Grade</td>  
  10.             </tr>  
  11.         </thead>  
  12.         <tbody>  
  13.             <tr *ngFor="let student of studentList;" [ngSwitch]="student.Grade">  
  14.                 <td>  
  15.                     <span *ngSwitchCase="'A'" [ngStyle]="{'font-size':'18px','color':'red'}">{{student.SrlNo}}</span>  
  16.                     <span *ngSwitchCase="'B'" [ngStyle]="{'font-size':'16px','color':'blue'}">{{student.SrlNo}}</span>  
  17.                     <span *ngSwitchCase="'C'" [ngStyle]="{'font-size':'14px','color':'green'}">{{student.SrlNo}}</span>  
  18.                     <span *ngSwitchDefault [ngStyle]="{'font-size':'12px','color':'black'}">{{student.SrlNo}}</span>  
  19.                 </td>  
  20.                 <td>  
  21.                     <span *ngSwitchCase="'A'" [ngStyle]="{'font-size':'18px','color':'red'}">{{student.Name}}</span>  
  22.                     <span *ngSwitchCase="'B'" [ngStyle]="{'font-size':'16px','color':'blue'}">{{student.Name}}</span>  
  23.                     <span *ngSwitchCase="'C'" [ngStyle]="{'font-size':'14px','color':'green'}">{{student.Name}}</span>  
  24.                     <span *ngSwitchDefault [ngStyle]="{'font-size':'12px','color':'black'}">{{student.Name}}</span>  
  25.                 </td>  
  26.                 <td>  
  27.                     <span *ngSwitchCase="'A'" [ngStyle]="{'font-size':'18px','color':'red'}">{{student.Course}}</span>  
  28.                     <span *ngSwitchCase="'B'" [ngStyle]="{'font-size':'16px','color':'blue'}">{{student.Course}}</span>  
  29.                     <span *ngSwitchCase="'C'" [ngStyle]="{'font-size':'14px','color':'green'}">{{student.Course}}</span>  
  30.                     <span *ngSwitchDefault [ngStyle]="{'font-size':'12px','color':'black'}">{{student.Course}}</span>  
  31.                 </td>  
  32.                 <td>  
  33.                     <span *ngSwitchCase="'A'" [ngStyle]="{'font-size':'18px','color':'red'}">{{student.Grade}}</span>  
  34.                     <span *ngSwitchCase="'B'" [ngStyle]="{'font-size':'16px','color':'blue'}">{{student.Grade}}</span>  
  35.                     <span *ngSwitchCase="'C'" [ngStyle]="{'font-size':'14px','color':'green'}">{{student.Grade}}</span>  
  36.                     <span *ngSwitchDefault [ngStyle]="{'font-size':'12px','color':'black'}">{{student.Grade}}</span>  
  37.                 </td>  
  38.             </tr>  
  39.         </tbody>  
  40.     </table>  
  41. </div> 
Step 3

Final code of app.module.ts file,
  1. import { NgModule } from '@angular/core';  
  2. import { FormsModule  } from '@angular/forms';  
  3. import { BrowserModule } from '@angular/platform-browser';  
  4.   
  5. import { ParentComponent } from './src/app.component.parent';  
  6. import { NgIfComponent } from './src/app.component.ngIf';  
  7. import { NgForComponent } from './src/app.component.ngFor';  
  8. import { NgSwitchComponent } from './src/app.component.ngSwitch';  
  9.   
  10.   
  11. @NgModule({  
  12.     imports: [BrowserModule, FormsModule],  
  13.     declarations: [ParentComponent, NgIfComponent, NgForComponent, NgSwitchComponent],  
  14.     bootstrap: [ParentComponent]  
  15. })  
  16. export class AppModule { }   
Step 4 

Final code of app.component.parent.html file,
  1. <div>  
  2.     <h2 class="badge">Structural Directives Demonstrate</h2>  
  3.     <br />  
  4.     <toggle-text></toggle-text>  
  5.     <br />  
  6.     <product-list></product-list>  
  7.     <br />  
  8.     <student-list></student-list>  
  9. </div> 
Now run the code and output as below,

Up Next
    Ebook Download
    View all
    Learn
    View all