Introduction
In this article, you will see the Looping Built-in Directives like NgFor, Getting an Index and NgNonBindable along with the examples.
Article Overview
- Background
- Prerequisites
- NgFor
- Getting an Index
- NgNonBindable
- Summary
Background
- Build-in directives are attributes which we add to HTML element to give dynamic behaviour.
- Angular provides various built-in directives.
- In this article we’re going to cover conditional directives such as NgFor, Getting an Index and NgNonBindable along with examples of how to use them.
Prerequisites
- You should have a basic knowledge of Angular.
NgFor
It is used to repeat a given DOM element (or a collection of DOM elements) and pass an element of the array on each iteration.
Syntax
*ngFor="let item of items"
- let item syntax specifies a (template) variable that’s receiving each element of the items array.
- items is the collection of items from your controller.
Basic Simple Example
Declare an array of members on our component controller,
- this.members = ['Sachin', Rahul', 'Mike'];
In template use it with the following HTML snippet,
- <h4 class="ui horizontal divider header">List of members</h4>
- <div class="ui list" *ngFor="let m of members">
- <div class="item">{{ m }}</div>
- </div>
Example of Iterate through an Array of Objects
Declare an array of objects of members on our component controller,
- this.members = [
- { name: 'Sachin', age: 25, city: 'Mumbai' },
- { name: 'Rahul', age: 22, city: 'Delhi' },
- { name: 'Mike', age: 24, city: 'London' }
- ];
Now, render the data,
- <h4 class="ui horizontal divider header">List of members</h4>
- <table class="ui celled table">
- <thead>
- <tr>
- <th>Name</th>
- <th>Age</th>
- <th>City</th>
- </tr>
- </thead>
- <tr *ngFor="let m of members">
- <td>{{ m.name }}</td>
- <td>{{ m.age }}</td>
- <td>{{ m.city }}</td>
- </tr>
- </table>

Nelson ChristianPosted Jul 2, 2021, 1:48 PM
Nice one! Very useful!!
Rushang VoraPosted Jul 1, 2021, 9:40 AM
Very Helpful
Arvind PatelPosted Jul 1, 2021, 8:01 AM
Useful Article.