ngFor Directive 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 *ngFor directive.

*ngFor

The ngFor directive repeats a template once per item in collection. It is very similar to ngReate directive in AngualrJS 1.x. Here, each template has their own scope in which given loop variable is set to the current collection item. It has a special set of variables properties , which can be useful while iterating with the collection.

When any changes made in the iterator, ngFor directive made corresponding changes to DOM i.e if any item is added, the new template instance is added in to the DOM. Similarly, If any item is removed, the template instance is removed from the DOM. If items are reordered, the respective templates are reordered in DOM.

Syntax

<<htmlelement> *ngFor="let item of items; let i = index; trackBy: trackByFn">...</< htmlelement> >

OR

<<htmlelement> template="ngFor let item of items; let i = index; trackBy: trackByFn">... </<htmlelement> >

Here, we can use any HTML with *ngFor like tr, li etc.

The asterisk (*) effect

With this directive, we were using asterisk (*) as a prefix. Angular 2 replaces asterisk (*) with verbose <template> form. Thus, we either need used asterisk (*) with this directive or we need to use this directive with the template. The example mentioned below covers both the scenario.

Example

In the example mentioned below, I have two array variables at my component and I have to print the data of the array, using ngFor directive.

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.     testArray:Array<any> =[{id:1, name:'Test 1'},{id:2, name: 'Test 2'},{id:3, name: 'Test 3'}];  
  8.   empDataArray:Array<any> =[{id:1, name:'Jignesh'},{id:2, name: 'Tejas'},{id:3, name: 'Rakesh'}];  
  9. }

directiveExample.html

  1. <h4>*ngFor in Angular 2 Application</h4>  
  2. <div>  
  3.     <h4>*ngFor Example</h4>  
  4.     <table border="1" width="20%">  
  5.         <tr>  
  6.             <th width="50%">Id</th>  
  7.             <th width="50%">Name</th>  
  8.         </tr>  
  9.         <tr *ngFor="let item of testArray;">   
  10.             <td>{{item.id}}</td>  
  11.             <td>{{item.name}}</td>  
  12.         </tr>  
  13.     </table>  
  14. </div>  
  15.   
  16. <div>  
  17.     <h4>ngFor Example with template</h4>  
  18.     <table border="1" width="20%">  
  19.         <tr>  
  20.             <th width="50%">Id</th>  
  21.             <th width="50%">Name</th>  
  22.         </tr>  
  23.         <tr template="ngFor let item of empDataArray;">   
  24.             <td>{{item.id}}</td>  
  25.             <td>{{item.name}}</td>  
  26.         </tr>  
  27.     </table>  
  28. </div>

Output



This directive has special variables/properties, which are shown below.

  1. Index

    It will set an integer value to the current loop, which indicates each item no. of the template context.

    directiveExample.html
    1. <div>  
    2.     <h4>ngFor Example -- index</h4>  
    3.     <table border="1" width="20%">  
    4.         <tr>  
    5.             <th width="50%">Id</th>  
    6.             <th width="50%">Name</th>  
    7.         </tr>  
    8.         <tr template="ngFor let item of empDataArray;let i=index">   
    9.             <td>{{i+1}}</td>  
    10.             <td>{{item.name}}</td>  
    11.         </tr>  
    12.     </table>  
    13. </div>
    Output



  2. First

    It is a Boolean variable and set to true, if the item is the first in iteration The code snippet mentioned below is used to hide first element of an array.

    directiveExample.html
    1. <div>  
    2.     <h4>ngFor Example -- first</h4>  
    3.     <table border="1" width="20%">  
    4.         <tr>  
    5.             <th width="50%">Id</th>  
    6.             <th width="50%">Name</th>  
    7.         </tr>  
    8.         <tr *ngFor="let item of empDataArray; #first=first" [hidden]="first">   
    9.             <td>{{item.id}}</td>  
    10.             <td>{{item.name}}</td>  
    11.         </tr>  
    12.     </table>  
    13. </div>
    Output



  3. last

    It is a Boolean variable and set to true, if an item is the last one in iteration The code snippet, mentioned below is used to hide last element of an array.

    directiveExample.html
    1. <div>  
    2.     <h4>ngFor Example -- last</h4>  
    3.     <table border="1" width="20%">  
    4.         <tr>  
    5.             <th width="50%">Id</th>  
    6.             <th width="50%">Name</th>  
    7.         </tr>  
    8.         <tr *ngFor="let item of empDataArray; #last=last" [hidden]="last">   
    9.             <td>{{item.id}}</td>  
    10.             <td>{{item.name}}</td>  
    11.         </tr>  
    12.     </table>  
    13. </div>
    Output



  4. even

    It is a Boolean variable and set to true when an item index is even. The code snippet, mentioned below is used to show only an even index element of an array.

    directiveExample.html
    1. <div>  
    2.     <h4>ngFor Example -- even</h4>  
    3.     <table border="1" width="20%">  
    4.         <tr>  
    5.             <th width="50%">Id</th>  
    6.             <th width="50%">Name</th>  
    7.         </tr>  
    8.         <tr *ngFor="let item of empDataArray; #even=even" [hidden]="even">   
    9.             <td>{{item.id}}</td>  
    10.             <td>{{item.name}}</td>  
    11.         </tr>  
    12.     </table>  
    13. </div> 
    Output



  5. odd

    It is a Boolean variable and set to true when an item index is odd. The code snippet, mentioned below is used to show only odd index element of an array.

    directiveExample.html
    1. <div>  
    2.     <h4>ngFor Example -- odd</h4>  
    3.     <table border="1" width="20%">  
    4.         <tr>  
    5.             <th width="50%">Id</th>  
    6.             <th width="50%">Name</th>  
    7.         </tr>  
    8.         <tr *ngFor="let item of empDataArray; #odd=odd" [hidden]="odd">   
    9.             <td>{{item.id}}</td>  
    10.             <td>{{item.name}}</td>  
    11.         </tr>  
    12.     </table>  
    13. </div> 
    Output


Conclusion

This article will help you to learn ngFor directive in Angular 2. It has a set of special variables/properties, which is useful in iterating the collection.

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.