Table of contents
1.
Introduction
2.
Understanding the @for Loop
2.1.
Key Features
2.2.
Syntax
3.
Contextual Variables
3.1.
Example:
4.
Steps to Implement @for Loop
4.1.
Step 1: Create an Angular Component
4.2.
Step 2: Define an Array in the Component
4.3.
Step 3: Use the @for Loop in the Template
4.4.
Step 4: Run the Application
5.
Example 1: Simple for loop
5.1.
Component (TypeScript)
5.2.
Template (HTML)
6.
Example 2: @for Loop with Track as Index
6.1.
Component (TypeScript)
6.2.
Template (HTML)
7.
Frequently Asked Questions
7.1.
What is the purpose of the @for loop in Angular?
7.2.
How does track improve performance in the @for loop?
7.3.
Can we use contextual variables inside the @for loop?
8.
Conclusion
Last Updated: Feb 19, 2025
Easy

for loop in Angular

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The *ngFor directive in Angular is used to iterate over arrays and dynamically generate elements in templates. It is commonly applied in lists, tables, and repeated UI components. This directive improves performance and simplifies handling dynamic data.

for loop in Angular

In this article, we will discuss the *ngFor directive, its syntax, contextual variables, best practices, and examples to help you implement loops effectively in Angular templates.

Understanding the @for Loop

The @for loop in Angular  is used inside templates to iterate over collections, such as arrays. It is particularly useful when dynamically rendering lists in an Angular component.

Key Features

  • Simplifies iteration over arrays.
     
  • Optimizes rendering performance.
     
  • Provides contextual variables like index and track.
     
  • Works efficiently with Angular signals and state management.

Syntax

Here is the basic syntax of the @for loop in Angular:

@for (item of items; track item) {
    <p>{{ item }}</p>
}

 

Explanation:

  • item represents the current element in the array.
     
  • items is the array being iterated over.
     
  • track item helps  Angular efficiently update the DOM by tracking each element.

Contextual Variables

 Angular provides contextual variables within the @for loop that help manage and track iteration more effectively:

  1. index: Represents the current index in the loop.
     
  2. count: Provides the total number of items in the array.
     
  3. first: Returns true for the first item in the iteration.
     
  4. last: Returns true for the last item in the iteration.
     
  5. odd: Returns true for odd index numbers.
     
  6. even: Returns true for even index numbers.

Example:

@for (item of items; track item) {
    <p>Item: {{ item }} - Index: {{ index }}</p>
}

Steps to Implement @for Loop

Step 1: Create an Angular Component

First, create a new  Angular component if you haven't already:

ng generate component for-loop-example

Step 2: Define an Array in the Component

In for-loop-example.component.ts, define an array:

import { Component } from '@angular/core';

@Component({
  selector: 'app-for-loop-example',
  templateUrl: './for-loop-example.component.html',
  styleUrls: ['./for-loop-example.component.css']
})
export class ForLoopExampleComponent {
  items: string[] = ['Angular', 'React', 'Vue', 'Svelte'];
}

Step 3: Use the @for Loop in the Template

In for-loop-example.component.html, implement the loop:

@for (item of items; track item) {
    <p>{{ item }}</p>
}

Step 4: Run the Application

Save the files and run your Angular application using:

ng serve

Example 1: Simple for loop

In this example, we will use the @for loop to display a list of programming languages.

Component (TypeScript)

import { Component } from '@angular/core';

@Component({
  selector: 'app-simple-loop',
  templateUrl: './simple-loop.component.html',
  styleUrls: ['./simple-loop.component.css']
})
export class SimpleLoopComponent {
  languages: string[] = ['JavaScript', 'Python', 'Java', 'C++'];
}

Template (HTML)

@for (language of languages; track language) {
    <p>Programming Language: {{ language }}</p>
}

 

Output

Programming Language: JavaScript
Programming Language: Python
Programming Language: Java
Programming Language: C++

 

Example 2: @for Loop with Track as Index

In this example, we will use a track with an index for better efficiency.

Component (TypeScript)

import { Component } from '@angular/core';

@Component({
  selector: 'app-track-example',
  templateUrl: './track-example.component.html',
  styleUrls: ['./track-example.component.css']
})
export class TrackExampleComponent {
  students = [
    { id: 1, name: 'Alice' },
    { id: 2, name: 'Bob' },
    { id: 3, name: 'Charlie' }
  ];
}

 

Template (HTML)

@for (student of students; track student.id) {
    <p>ID: {{ student.id }} - Name: {{ student.name }}</p>
}

 

Output

ID: 1 - Name: Alice
ID: 2 - Name: Bob
ID: 3 - Name: Charlie

Frequently Asked Questions

What is the purpose of the @for loop in Angular?

The @for loop helps iterate over collections and render elements dynamically in Angular templates.

How does track improve performance in the @for loop?

Tracking elements ensuresAngular  efficiently updates the DOM by keeping track of each element, reducing unnecessary re-renders.

Can we use contextual variables inside the @for loop?

Yes, Angular provides variables like index, count, first, last, odd, and even to help manage iteration effectively.

Conclusion

In this article, we covered on how to use the for loop in Angular with the *ngFor directive to iterate over arrays and display dynamic content in templates. The *ngFor directive simplifies rendering lists efficiently. Understanding how to use it helps in building dynamic, interactive, and well-structured Angular applications with better data handling.

Live masterclass