Angular ngFor Directive

Angular ngFor Directive

·

3 min read

The Angular ngFor directive is a structural directive that allows you to loop over a list of items and render them in the template. It is similar to the for loop in JavaScript, but it is optimized for rendering lists in the template.

Using the ngFor Directive

To use the ngFor directive, you have to first bind it to an array of items in your component class. Then, you can use the directive in the template to loop over the items and render them.

Here is an example of how to use the ngFor directive:

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

@Component({
  selector: 'app-root',
  template: `
    <ul>
      <li *ngFor="let item of items">{{ item }}</li>
    </ul>
  `
})
export class AppComponent {
  items = ['item 1', 'item 2', 'item 3'];
}

In the template, the ngFor directive is used with the * symbol to indicate that it is a structural directive. The ngFor directive is then followed by the let keyword, which defines a template input variable that you can use to access the current item in the loop. In this example, the template input variable is called item.

The ngFor directive also has a number of optional parameters that you can use to customize the loop. For example, you can use the index parameter to get the index of the current item in the loop:

<ul>
  <li *ngFor="let item of items; index as i">{{ i }}: {{ item }}</li>
</ul>

You can also use the first, last, and odd parameters to apply styles to the first, last, or odd-numbered items in the list:

<ul>
  <li *ngFor="let item of items; first as isFirst">
    <span *ngIf="isFirst">First item:</span> {{ item }}
  </li>
</ul>

The ngFor TrackBy Function

When the list of items changes, Angular has to update the DOM to reflect the changes. This can be expensive if the list is large or the items have complex templates. To optimize this process, you can use the ngFor directive's trackBy function to tell Angular how to identify items in the list.

The trackBy function takes an index and the current item as arguments, and returns a unique identifier for the item. Angular uses this identifier to track items in the list and optimize the update process.

Here is an example of how to use the trackBy function:

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

@Component({
  selector: 'app-root',
  template: `
    <ul>
      <li *ngFor="let item of items; trackBy: trackByFn">{{ item.name }}</li>
    </ul>
  `
})
export class AppComponent {
  items = [
    { id: 1, name: 'Item 1' },
    { id: 2, name: 'Item 2' },
    { id: 3, name: 'Item 3' }
  ];

  trackByFn(index: number, item: any) {
return item.id;
}
}

In this example, the trackByFn function returns the id property of each item as the unique identifier. This tells Angular to track items in the list based on their id property, and optimize the update process accordingly.

Conclusion

The Angular ngFor directive is a powerful tool for rendering lists in the template. It allows you to loop over an array of items and render them in the template, and provides a number of optional parameters for customizing the loop. You can also use the trackBy function to optimize the update process when the list of items changes.