Learn Angular *ngFor Directive

Introduction

*ngFor is a structural and built-in directive that is used to iterate over collections like an array and create a template for each item.

What is Structural Directive?

A structural directive adds and removes elements in the DOM tree. Here, *ngFor does the same.

Basic Syntax of ngFor

  1. <tr *ngFor="let s of students">  
  2.    <td>{{s.Name}}</td>  
  3. </tr>  

Example

Let’s take an example and understand this with the help of it - Suppose we have list of student objects with some of their information, like StudentID, Name, DOB, StuClass, Gender, Age & Address.

  1. [  
  2.    { studentID: 1, studentName: 'Steve', gender: 'Male', age: 35, course: 'MCA' },  
  3.    { studentID: 2, studentName: 'Bobby', gender: 'Male', age: 32, course: 'MBA' },  
  4.    { studentID: 3, studentName: 'Rina', gender: 'Female', age: 45, course: 'B.Tech' },  
  5.    { studentID: 4, studentName: 'Alex', gender: 'Female', age: 24, course: 'M.Tech' },  
  6. ]  

And, we want to have this data in HTML table format, like below.

Angular

This is how we can do using ngFor directive introduced in Angular 2.

Let’s look at it via code.

Step 1

Let’s create a folder named Student under src/app folder.

Angular

Add a TypeScript file named student.component.ts under this folder and add the below code into it.

  1. import {Component} from '@angular/core'  
  2.   
  3. @Component({  
  4.     selector: 'list-student',  
  5.     templateUrl: 'app/student/student.component.html',  
  6.     styles: [  
  7.                 `  
  8.                     td {  
  9.                             border: 1px solid black;  
  10.                             padding:4px;  
  11.                     }  
  12.                     th {  
  13.                         border: 1px solid black;  
  14.                         padding:4px;  
  15.                     }  
  16.                 `  
  17.             ]  
  18. })  
  19. export class StudentListComponent {  
  20.     students: any[] =   
  21.  [  
  22.    { studentID: 1, studentName: 'Steve', gender: 'Male', age: 35, course: 'MCA' },  
  23.    { studentID: 2, studentName: 'Bobby', gender: 'Male', age: 32, course: 'MBA' },  
  24.    { studentID: 3, studentName: 'Rina', gender: 'Female', age: 45, course: 'B.Tech' },  
  25.    { studentID: 4, studentName: 'Alex', gender: 'Female', age: 24, course: 'M.Tech' },  
  26.  ];  
  27. }  

For making the class named StudentistComponent an Angular component, I decorated it with component decorator by importing component from Angular core.

templateUrl 

For using view of this component, I used templateUrl property to assign an external HTML file.

Styles 

For applying CSS styling over table output, I included some style code under styles property. We can use also styleUrls property in case we want to use some external CSS file for styling.

Let’s try to understand some common things using below image,

Angular

Step 2

Let’s add a view file named student.component.html under student folder,

Angular

And add the below HTML code into this this file,

  1. <table>  
  2.     <thead>  
  3.         <tr>  
  4.             <th>Student ID</th>  
  5.             <th>Name</th>  
  6.             <th>Gender</th>  
  7.             <th>Age</th>  
  8.             <th>Course</th>  
  9.         </tr>  
  10.     </thead>  
  11.     <tbody>  
  12.         <tr *ngFor="let s of students">  
  13.             <td>{{s.studentID}}</td>  
  14.             <td>{{s.studentName}}</td>  
  15.             <td>{{s.gender}}</td>  
  16.             <td>{{s.age}}</td>  
  17.             <td>{{s.course}}</td>  
  18.         </tr>  
  19.     </tbody>  
  20. </table>  

*ngFor

It’s a built-in directive that is used to iterate over student list and add student item in the DOM tree.

Step 3

Under app folder, open app.component.ts file and add the list-student directive as view, 

  1. import { Component } from '@angular/core';  
  2.   
  3. @Component({  
  4.   selector: 'my-app',  
  5.   template: `<list-student></list-student>`,  
  6. })  
  7. export class AppComponent {  
  8.     name = 'Angular2 Student Demo';  
  9. }   

Step 4

Under the same app folder, open app.module.ts file and import the student component as well as add your component under declarations like below,

  1. import { NgModule }      from '@angular/core';  
  2. import { BrowserModule } from '@angular/platform-browser';  
  3.   
  4. import { AppComponent } from './app.component';  
  5. import { StudentListComponent } from './student/student.component'  
  6.   
  7. @NgModule({  
  8.     imports: [BrowserModule],  
  9.     declarations: [AppComponent, StudentListComponent],  
  10.   bootstrap:    [ AppComponent ]  
  11. })  
  12. export class AppModule { }  

Step 5

Now, our application is ready to show the expected output. Let’s run our project using F5 and see the output in browser.

Angular

Note

To setup Angular2 project in your Visual Studio, download the "Quick Start Project" from the given link and use the contents from downloaded folder into your project.

Write me in comment box in case you want some help in setting up Angular 2 into your Visual Studio.


Similar Articles