How To Use ngFor Directive In Angular

What is ngFor?

ngFor is a directive that is used for iteration on the collection of data. ngFor has inbuilt variables which help to detect Index, First, Last anFirst, Even items at runtime.

ngFor is used with any HTML element but mostly we use it in those elements which help to display the data repetitively.

ngFor Syntax 1

The following syntax is very simple and similar to ForEach loop in C# and VB.NET.

*ngFor =”let <single object/item> of <collection object>”

Example

*ngFor = “let member of memberlist”

ngFor Directive

ngFor Syntax 2

The following syntax is very simple and is also, similar to ForEach loop in C# and VB.NET.

*ngFor =”let <single object/item> of <collection object>; let <variable name> = index”

Example

*ngFor = “let member of memberlist; let srno = index”

ngFor Directive

Step by step event binding in Angular using Visual Studio Code.

Step by step Implementation  Syntax Format 1

  1. Click on Node.js command prompt to proceed.

    ngFor Directive
  1. Create a project folder named ”MemberAng” in the command prompt.

    ngFor Directive

Now, change the directory to newly created directory/folder called “MemberAng”.

ngFor Directive

Now, we pass command to create an Angular project called “membersample”.

ngFor Directive

After pressing the Enter key on command, the below given output screen will display.

 ngFor Directive

In the above screenshot, you can see the message in the green colour [ Project “membersample” successfully created].

  1. Now, switch to Visual Studio Code and open the following folder.

    ngFor Directive

D:\Angular4\MemberAng\MemberSample

Open this folder in Visual Studio.

ngFor Directive
  1. After opening the folder in Visual Studio Code, next, you can directly this folder from the recently opened folder list.

    ngFor Directive

  2. By default, our project will look like this in visual studio code as per the screenshot.

    ngFor Directive 

No file opened by default.

  1. Now, again switch to command prompt.

In command prompt, we pass the command to OPEN (EXECUTE) our project with following CLI command.

ng serve open [PRESS ENTER]

 ngFor Directive

As you pressed enter key in console window, it generates the following screen and hint to see your project on browser.

In browser, just type - localhost:4200

ngFor Directive
  1. Open you browser (My browser is Mozilla Developer Edition) type: localhost:4200 in addressbar.

    ngFor Directive

Till the above steps, you have created only empty Angular project. Now, let us start to implement ngFor in this project.

  1. First, create a class called “member”.

 ngFor Directive

To create a class

Syntax
ng g class <class name>

Example
ng g class member

You can see newly created member.ts class in

 ngFor Directive

Double click on member.ts class and insert following into class file.

  1. export class member {  
  2.     _membername: string;  
  3.     _phone: string;  
  4.     _place: string;  
  5.     constructor(membername: string, phone: string, place: string) {  
  6.         this._membername = membername;  
  7.         this._phone = phone;  
  8.         this._place = place;  
  9.     }  
  10. }  
ngFor Directive
  1. Now switch to component.ts file and create array of class member with hard coded data.

First write import statement to get reference of MEMBER class.

  1. import {member} from './member';  

Second Hard coded array data,

  1. memberlist = [  
  2.         new member('Sai''98231231''Shirdi-Ahmednagar'),  
  3.         new member('Sairam''654654564''Malad-Mumbai'),  
  4.         new member('Ashish''987987987''Jodhpur-Rajasthan'),  
  5.         new member('Suhana''125986987''Phalodi-Jodhpur'),  
  6.         new member('Nirupama''987987987', 'Mumbai)];  
  1. Now switch to component.html file

 Remove all default html code of file.

  1. <ul>  
  2.     <li *ngFor="let member of memberlist"> {{member._membername}} -- {{member._phone}} -- {{member._place}} </li>  
  3. </ul>  
  1. Now check your browser for output.

Output

 ngFor Directive

Step by step Implementation  Syntax Format 2

  1. To achieve output as per uses of Syntax Format 2 we have to do only changes in html code.

switch to app.component.html file

 Remove all default html code of file.

  1. <ul>  
  2.     <li *ngFor="let member of memberlist;let srno = index"> {{srno}} {{member._membername}} -- {{member._phone}} -- {{member._place}} </li>  
  3. </ul>  
  1. Check your browser for output

    OUTPUT

    ngFor Directive

As you can see index number started from zero(0) number. To get first number as 1(one) change html code as following:

  1. <ul>  
  2.     <li *ngFor="let member of memberlist;let srno = index"> {{srno+1}} {{member._membername}} -- {{member._phone}} -- {{member._place}} </li>  
  3. </ul>  

We had just add +1 in srno variable to get start index number from numeric 1.

OUTPUT

 

ngFor Directive

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now