Explain ngFor trackBy in Angular

What is ngFor in Angular Template syntax?

  • Angular makes use of HTML for templates associated with components that eventually represent the views of your front-end application.
  • A component controls the part of the user’s application UI and the associated template is what gets rendered in that part of the User Interface of an Application. The simple file structure of Angular makes it much simple to handle these kind of actions.
  • The angular ngFor trackBy is used to improve the performance of an angular application. At the
  • end of this blog, you will understand how to use ngFor trackBy and when to use the ngFor trackBy in your angular application.
  • For HTML does not have any built-in template language, Angular extends HTML with a powerful template syntax that includes many directives such as ngFor is similar to for-loops in programming languages.
  • The ngFor allows you to loop between an array of data directly in the HTML template. The array should be present in the connected component.

How to Use ngFor in Angular Templates?

Prerequisites

  • Before creating the following application, we need the basic knowledge of angular and a few prerequisites as shown below:
    • First of all, we need the basic knowledge of Angular and how does it work.
    • Your computer must have Visual Studio Code installed and updated to the latest version.
    • Your computer must have the Angular Command Line Interface installed to the latest version.
    • Your computer must have the Node JS installed to the latest version.

Step 1: app.component.ts file

  • Now we see how to use the ngFor directive by example.
  • Now as you see below code, in the constructor of the app component class we initialize the student’s collection property with three students’ objects.
  • In getStudent() method of app component class update the same student’s collection with five student objects.
  • Here it is:

Step 2: app.component.scss file

  • Now we add some style to our HTML page.

 

Step 3: app.module.ts file

  • Now we add some modules in app.module.ts file.

Step 4: app.component.html file

  • Now we add our HTML code in the app.component.html file.

  • In the above code, we are not using the trackBy with ngFor directive.
  • When your page loads for the first time you will see three students who initialize the constructor of the app component class and when you click on the refresh button, then you will see the fourth and fifth students in your table.

 

Why do we need to use ngFor trackBy in the angular application?

  • The trackBy used to improve the performance of the angular project.
  • It is usually not needed only when your application running into performance issues.
  • The angular ngFor directive may perform poorly with large applications.
  • A little change to the collection is adding a new item or removing an existing item from the collection may trigger a cascade of DOM manipulations.
  • Assume, we have some data coming from some back-end API and we are storing data into some type of collection like an array and then we need to update these data over the webpage using ngFor directive.
  • By default, what the angular framework will do is, it will remove all the DOM elements that are associated with the data and will create them again in the DOM tree even if the equal data is coming.
  • A lot of DOM Manipulation will occur in the background if a large amount of data coming from the back-end API repeatedly.

 

The ngFor with TrackBy with example

  • Angular gives the trackBy feature which allows you to track elements when they are added or removed from the array for performance reasons.
  • We simply need to define a trackBy method, which needs to identify each element uniquely, in the associated component and assign it to the ngFor directives as follows:

  • We can simply identify each element in a unique way using its id.

Conclusion

In this article, we discussed the ngFor trackBy feature of the angular framework which is widely used incorporates to track elements when they are removed or added from the specific array for performance reasons. In other words, we can identify each element uniquely by its ID. We also discussed how to use it stepwise, the need for this feature, and an example of the same.