Angular: the ngFor directive

Angular: the ngFor directive

Among the many features offered by Angular, the *ngFor directive is one of the most used and powerful when it comes to managing iterations and rendering of lists of data.

Angular is a powerful and flexible front-end framework developed by Google, which gives developers the tools needed to create dynamic and scalable web applications. Among the many features offered by Angular, the *ngFor directive is one of the most used and powerful when it comes to managing iterations and rendering of lists of data.

Introduction to the *ngFor Directive

The *ngFor directive is a core Angular construct that allows you to iterate over a set of data and dynamically generate corresponding HTML elements. This is especially useful when you want to display lists of data, tables, or any other repetitive structure within the user interface.

To use the *ngFor directive, you must apply it to an HTML element and provide an iteration statement. The iteration statement can be an array, an iterable object, or other data types that can be iterated.

Basic Syntax

The basic syntax of the *ngFor directive is as follows:


<div *ngFor="let item of items; let i = index">
   {{ i + 1 }}. {{ item }}
</div>

In this example, items is the array you are iterating over. The variable item represents the current item in the iteration, while i represents the index of the item. Using let i = index is optional and is used to get the index of the element in the iteration.

Object and Map Management

The *ngFor directive can also be used with objects and maps. For example:


<div *ngFor="let key of Object.keys(myObject)">
   {{ key }}: {{ myObject[key] }}
</div>

In this example, myObject is an object, and Object.keys(myObject) returns an array of the object's keys. We then iterate over this array of keys, accessing the corresponding values in the object.

Management of Anidated Iterations

Angular also allows nested iterations, allowing you to easily handle situations where you need to iterate over multiple layers of data. Here's an example:


<div *ngFor="let category of categories">
   <h2>{{ category.name }}</h2>
   <ul>
     <li *ngFor="let product of category.products">
       {{ product.name }} - {{ product.price }}
     </li>
   </ul>
</div>

In this case, we iterate over an array of categories, and within each category we iterate over the arrays of associated products.

Using *ngFor with Change traces

Angular offers the ability to improve performance through the use of change tracks (trackBy). This allows the framework to efficiently identify elements that have been changed, added, or removed during an iteration. Here is an example of use:


<div *ngFor="let item of items; trackBy: trackByFn">
   {{ item }}
</div>

In the TypeScript component, you need to define the trackByFn:

function

trackByFn(index, item) {
   return item.id; // It is assumed that there is a unique "id" field for each element
}

Conclusions

Angular's *ngFor directive is a powerful tool that makes managing iterations in HTML templates much easier. With its flexibility and ease of use, it is an indispensable tool for creating dynamic and responsive user interfaces. Whether you're displaying lists of data, tables, or more complex structures, *ngFor provides an elegant way to manage iterations within Angular applications.