Jenna 009

Jenna 009

1584969840

How to Use TrackBy in Angular 8 application

In this article, we are going to learn how to use Track By in Angular applications.

In every application, to run the application faster we need to check the performance of our application. A ngular provides a method called trackBy which is used to track our incoming data every time we request from API.

Suppose we have some data coming from API request into the collection and we need to change the data over the web page using ngFor directive. In this case Angular will remove all the DOM elements that are associated with the data and will create them again in the DOM tree. That means a lot of DOM manipulations will happen if a large amount of data is coming from the API.

Prerequisites

  • Basic knowledge of Angular
  • Visual Studio Code must be installed
  • Angular CLI must be installed
  • Node JS must be installed

Step 1

Let’s create a new Angular project using the following NPM command,

ng new trackBy   

Step 2

Now, let’s create a new component by using the following command,

ng g c trackBy-example  

Step 3

Now, open the trackBy-example.component.html file and add the following code in the file,

<h4 style="text-align: center;">{{SampleMessage}}</h4>    
<div class="row">    
  <div class="col-12 col-md-12">    
    <div class="card">    
      <div class="card-body position-relative">    
        <div class="table-responsive cnstr-record product-tbl">    
          <table class="table table-bordered heading-hvr">    
            <thead>    
              <tr>    
                <th width="50">Art.No </th>    
                <th>Brand</th>    
                <th>Price/Unit</th>    
                <th>Provider</th>    
                <th>P. Art. N</th>    
                <th>S. A/C</th>    
              </tr>    
            </thead>    
            <tbody>    
              <tr *ngFor="let product of companyProduct;">    
                <td align="center">{{product.ArtNo}}</td>    
                <td>{{product.Brand}}</td>    
                <td>{{product.Price }}</td>    
                <td>{{product.Provider}}</td>    
                <td>{{product.ProviderArtNo}}</td>    
                <td>{{product.SalesAccount}}</td>    
              </tr>    
            </tbody>    
          </table>    
          <button (click)='getNewCompanies()'>New Companies</button>    
        </div>    
      </div>    
    </div>    
  </div>    
</div>    

Here we are not using trackBy function yet.

In the below code we can check how to apply trackby with our structural direcrtive *ngFor

<tr *ngFor="let product of companyProduct; trackBy:trackByArtNo">  

Step 4

Now, open the trackBy-example.component.ts file and add the following code in this file,

import { Component, OnInit } from '@angular/core';  
import { ProductsService } from '../product.service';  
  
@Component({  
  selector: 'app-trackby',  
  templateUrl: './trackby.component.html'  
})  
export class TrackbyComponent implements OnInit {  
  companyProduct: any[];  
  SampleMessage="Example of Angular Fetching records using TrackBy";  
  
  constructor(private productService: ProductsService) {  
  
  }  
  ngOnInit() {  
    this.companyProduct = this.productService.getAllProductsUsingTrackBy();  
  }  
  
  getNewCompanies(): void {  
    this.companyProduct = this.productService.getAllProductsUsingTrackByExample();  
  }  
  trackByArtNo(index: number, companyProduct: any): string {  
    return companyProduct.ArtNo;  
  }  
  
}  

TrackBy function will take two arguments, the  first is index and the second is current item. We can return the unique identifier as a return argument.

trackByArtNo(index: number, companyProduct: any): string {    
   return companyProduct.ArtNo;    
 }  

Step 5

Now, open the product.service.ts file and add the following code,

import { Injectable } from '@angular/core';  
  
@Injectable()  
  
export class ProductsService {  
  employees: any[]; 
  
  constructor() {  
  }  
  
  getAllProductsUsingTrackBy() {  
    return this.employees = [  
      {  
        ProductId: 1,  
        ArtNo: "100",  
        Provider: "OppoProvider",  
        ProviderArtNo: "1Yu",  
        Brand: "Oppo",  
        Price: 7810.23,  
        BuyAccount: "123",  
        SalesAccount: "321"  
      },  
      {  
        ProductId: 1,  
        ArtNo: "101",  
        Provider: "OppoProvider",  
        ProviderArtNo: "1Yu",  
        Brand: "Oppo",  
        Price: 2310.23,  
        BuyAccount: "123",  
        SalesAccount: "321"  
      },  
      {  
        ProductId: 1,  
        ArtNo: "102",  
        Provider: "OppoProvider",  
        ProviderArtNo: "1Yu",  
        Brand: "Oppo",  
        Price: 7810.23,  
        BuyAccount: "123",  
        SalesAccount: "321"  
      },  
      {  
        ProductId: 1,  
        ArtNo: "103",  
        Provider: "OppoProvider",  
        ProviderArtNo: "1Yu",  
        Brand: "Oppo",  
        Price: 5810.23,  
        BuyAccount: "123",  
        SalesAccount: "321"  
      }  
    ];  
  }  
  
  getAllProductsUsingTrackByExample() {  
    return this.employees = [  
      {  
        ProductId: 1,  
        ArtNo: "100",  
        Provider: "OppoProvider",  
        ProviderArtNo: "1Yu",  
        Brand: "Oppo",  
        Price: 7810.23,  
        BuyAccount: "123",  
        SalesAccount: "321"  
      },  
      {  
        ProductId: 1,  
        ArtNo: "101",  
        Provider: "OppoProvider",  
        ProviderArtNo: "1Yu",  
        Brand: "Oppo",  
        Price: 2310.23,  
        BuyAccount: "123",  
        SalesAccount: "321"  
      },  
      {  
        ProductId: 1,  
        ArtNo: "102",  
        Provider: "OppoProvider",  
        ProviderArtNo: "1Yu",  
        Brand: "Oppo",  
        Price: 7810.23,  
        BuyAccount: "123",  
        SalesAccount: "321"  
      },  
      {  
        ProductId: 1,  
        ArtNo: "103",  
        Provider: "OppoProvider",  
        ProviderArtNo: "1Yu",  
        Brand: "Oppo",  
        Price: 5810.23,  
        BuyAccount: "123",  
        SalesAccount: "321"  
      },  
      {  
        ProductId: 1,  
        ArtNo: "104",  
        Provider: "OppoProvider",  
        ProviderArtNo: "1Yu",  
        Brand: "Oppo",  
        Price: 4770.23,  
        BuyAccount: "143",  
        SalesAccount: "211"  
      },  
    ];  
  }  
  
}  

Now it’s time to run the project by using ‘npm start’ or ‘ng serve’ command and check the output.

As in the below image when we click on “New Companies” it will add one new entry in the table .

This is image title

If we use normal *ngFor directive without using trackBy function Angular application will remove all the DOM elements and will recreate them again in the DOM tree, even if the same data is coming. This can slow down our application performance if the data is large in number.

This is image title

But with the help of trackBy it will not create a new DOM as we can see in the below image. It just gets added only the new data in the DOM which will increase the application performance.

This is image title

Conclusion

In this article, we have seen how to use trackBy with NgFor in Angular 8 Application.

Please give your valuable feedback/comments/questions about this article. Please let me know if you liked and understood this article and how I could improve it.

Thank you!

#angular #NgFor in Angular 8 #programming

What is GEEK

Buddha Community

How to Use TrackBy in Angular 8 application
Christa  Stehr

Christa Stehr

1598940617

Install Angular - Angular Environment Setup Process

Angular is a TypeScript based framework that works in synchronization with HTML, CSS, and JavaScript. To work with angular, domain knowledge of these 3 is required.

  1. Installing Node.js and npm
  2. Installing Angular CLI
  3. Creating workspace
  4. Deploying your First App

In this article, you will get to know about the Angular Environment setup process. After reading this article, you will be able to install, setup, create, and launch your own application in Angular. So let’s start!!!

Angular environment setup

Install Angular in Easy Steps

For Installing Angular on your Machine, there are 2 prerequisites:

  • Node.js
  • npm Package Manager
Node.js

First you need to have Node.js installed as Angular require current, active LTS or maintenance LTS version of Node.js

Download and Install Node.js version suitable for your machine’s operating system.

Npm Package Manager

Angular, Angular CLI and Angular applications are dependent on npm packages. By installing Node.js, you have automatically installed the npm Package manager which will be the base for installing angular in your system. To check the presence of npm client and Angular version check of npm client, run this command:

  1. npm -v

Installing Angular CLI

  • Open Terminal/Command Prompt
  • To install Angular CLI, run the below command:
  1. npm install -g @angular/cli

installing angular CLI

· After executing the command, Angular CLI will get installed within some time. You can check it using the following command

  1. ng --version

Workspace Creation

Now as your Angular CLI is installed, you need to create a workspace to work upon your application. Methods for it are:

  • Using CLI
  • Using Visual Studio Code
1. Using CLI

To create a workspace:

  • Navigate to the desired directory where you want to create your workspace using cd command in the Terminal/Command prompt
  • Then in the directory write this command on your terminal and provide the name of the app which you want to create. In my case I have mentioned DataFlair:
  1. Ng new YourAppName

create angular workspace

  • After running this command, it will prompt you to select from various options about the CSS and other functionalities.

angular CSS options

  • To leave everything to default, simply press the Enter or the Return key.

angular setup

#angular tutorials #angular cli install #angular environment setup #angular version check #download angular #install angular #install angular cli

Clara  Gutmann

Clara Gutmann

1598716260

Angular 8 CRUD Example | Angular 8 Tutorial For Beginners

Angular 8 CRUD is a basic operation to learn Angular from scratch. We will learn how to build a small web application that inserts, read data, update and delete data from the database. You will learn how to create a MEAN Stack web application. In this Angular 8 Tutorial Example, you will learn a new framework by building a crud application.

New features of Angular 8

You check out the new features in brief on my  Angular 8 New Features post.

I have designed this Angular 8 CRUD Tutorial, especially for newcomers, and it will help you to up and running with the latest version of Angular, which is right now 8.

#angular #angular 8 #angular 8 crud

Roberta  Ward

Roberta Ward

1595344320

Wondering how to upgrade your skills in the pandemic? Here's a simple way you can do it.

Corona Virus Pandemic has brought the world to a standstill.

Countries are on a major lockdown. Schools, colleges, theatres, gym, clubs, and all other public places are shut down, the country’s economy is suffering, human health is on stake, people are losing their jobs and nobody knows how worse it can get.

Since most of the places are on lockdown, and you are working from home or have enough time to nourish your skills, then you should use this time wisely! We always complain that we want some ‘time’ to learn and upgrade our knowledge but don’t get it due to our ‘busy schedules’. So, now is the time to make a ‘list of skills’ and learn and upgrade your skills at home!

And for the technology-loving people like us, Knoldus Techhub has already helped us a lot in doing it in a short span of time!

If you are still not aware of it, don’t worry as Georgia Byng has well said,

“No time is better than the present”

– Georgia Byng, a British children’s writer, illustrator, actress and film producer.

No matter if you are a developer (be it front-end or back-end) or a data scientisttester, or a DevOps person, or, a learner who has a keen interest in technology, Knoldus Techhub has brought it all for you under one common roof.

From technologies like Scala, spark, elastic-search to angular, go, machine learning, it has a total of 20 technologies with some recently added ones i.e. DAML, test automation, snowflake, and ionic.

How to upgrade your skills?

Every technology in Tech-hub has n number of templates. Once you click on any specific technology you’ll be able to see all the templates of that technology. Since these templates are downloadable, you need to provide your email to get the template downloadable link in your mail.

These templates helps you learn the practical implementation of a topic with so much of ease. Using these templates you can learn and kick-start your development in no time.

Apart from your learning, there are some out of the box templates, that can help provide the solution to your business problem that has all the basic dependencies/ implementations already plugged in. Tech hub names these templates as xlr8rs (pronounced as accelerators).

xlr8rs make your development real fast by just adding your core business logic to the template.

If you are looking for a template that’s not available, you can also request a template may be for learning or requesting for a solution to your business problem and tech-hub will connect with you to provide you the solution. Isn’t this helpful 🙂

Confused with which technology to start with?

To keep you updated, the Knoldus tech hub provides you with the information on the most trending technology and the most downloaded templates at present. This you’ll be informed and learn the one that’s most trending.

Since we believe:

“There’s always a scope of improvement“

If you still feel like it isn’t helping you in learning and development, you can provide your feedback in the feedback section in the bottom right corner of the website.

#ai #akka #akka-http #akka-streams #amazon ec2 #angular 6 #angular 9 #angular material #apache flink #apache kafka #apache spark #api testing #artificial intelligence #aws #aws services #big data and fast data #blockchain #css #daml #devops #elasticsearch #flink #functional programming #future #grpc #html #hybrid application development #ionic framework #java #java11 #kubernetes #lagom #microservices #ml # ai and data engineering #mlflow #mlops #mobile development #mongodb #non-blocking #nosql #play #play 2.4.x #play framework #python #react #reactive application #reactive architecture #reactive programming #rust #scala #scalatest #slick #software #spark #spring boot #sql #streaming #tech blogs #testing #user interface (ui) #web #web application #web designing #angular #coronavirus #daml #development #devops #elasticsearch #golang #ionic #java #kafka #knoldus #lagom #learn #machine learning #ml #pandemic #play framework #scala #skills #snowflake #spark streaming #techhub #technology #test automation #time management #upgrade

Benefits of Angular JS based Applications

AngularJS was introduced in the year 2009, by Google. AngularJS is a software framework used worldwide by developers. The entire base of this framework is open source. AngularJS has gained popularity among developers because of how it has become for them to create web applications. AngularJS helps in building apps that require less work and reduces the use of unnecessary codes. AngularJS application development is a javascript framework. AngularJS has a clear goal to make the entire process simpler, it also helps app development process and operations as much as it could. AngularJS is used for building applications that support MVC (model view controller) and SPAs (single page web apps) coding and programming structures. AngularJS has been used by some of the top companies in the world to simplify their app development process, like, Google, Paypal, Udemy, mobile site in iPad for HBO, etc. To read more click on the link.

#hire angular js developer #hire dedicated angular js developer #angular js application development #hire dedicated angular js team #hire best angular js application developer

Jenna 009

Jenna 009

1584969840

How to Use TrackBy in Angular 8 application

In this article, we are going to learn how to use Track By in Angular applications.

In every application, to run the application faster we need to check the performance of our application. A ngular provides a method called trackBy which is used to track our incoming data every time we request from API.

Suppose we have some data coming from API request into the collection and we need to change the data over the web page using ngFor directive. In this case Angular will remove all the DOM elements that are associated with the data and will create them again in the DOM tree. That means a lot of DOM manipulations will happen if a large amount of data is coming from the API.

Prerequisites

  • Basic knowledge of Angular
  • Visual Studio Code must be installed
  • Angular CLI must be installed
  • Node JS must be installed

Step 1

Let’s create a new Angular project using the following NPM command,

ng new trackBy   

Step 2

Now, let’s create a new component by using the following command,

ng g c trackBy-example  

Step 3

Now, open the trackBy-example.component.html file and add the following code in the file,

<h4 style="text-align: center;">{{SampleMessage}}</h4>    
<div class="row">    
  <div class="col-12 col-md-12">    
    <div class="card">    
      <div class="card-body position-relative">    
        <div class="table-responsive cnstr-record product-tbl">    
          <table class="table table-bordered heading-hvr">    
            <thead>    
              <tr>    
                <th width="50">Art.No </th>    
                <th>Brand</th>    
                <th>Price/Unit</th>    
                <th>Provider</th>    
                <th>P. Art. N</th>    
                <th>S. A/C</th>    
              </tr>    
            </thead>    
            <tbody>    
              <tr *ngFor="let product of companyProduct;">    
                <td align="center">{{product.ArtNo}}</td>    
                <td>{{product.Brand}}</td>    
                <td>{{product.Price }}</td>    
                <td>{{product.Provider}}</td>    
                <td>{{product.ProviderArtNo}}</td>    
                <td>{{product.SalesAccount}}</td>    
              </tr>    
            </tbody>    
          </table>    
          <button (click)='getNewCompanies()'>New Companies</button>    
        </div>    
      </div>    
    </div>    
  </div>    
</div>    

Here we are not using trackBy function yet.

In the below code we can check how to apply trackby with our structural direcrtive *ngFor

<tr *ngFor="let product of companyProduct; trackBy:trackByArtNo">  

Step 4

Now, open the trackBy-example.component.ts file and add the following code in this file,

import { Component, OnInit } from '@angular/core';  
import { ProductsService } from '../product.service';  
  
@Component({  
  selector: 'app-trackby',  
  templateUrl: './trackby.component.html'  
})  
export class TrackbyComponent implements OnInit {  
  companyProduct: any[];  
  SampleMessage="Example of Angular Fetching records using TrackBy";  
  
  constructor(private productService: ProductsService) {  
  
  }  
  ngOnInit() {  
    this.companyProduct = this.productService.getAllProductsUsingTrackBy();  
  }  
  
  getNewCompanies(): void {  
    this.companyProduct = this.productService.getAllProductsUsingTrackByExample();  
  }  
  trackByArtNo(index: number, companyProduct: any): string {  
    return companyProduct.ArtNo;  
  }  
  
}  

TrackBy function will take two arguments, the  first is index and the second is current item. We can return the unique identifier as a return argument.

trackByArtNo(index: number, companyProduct: any): string {    
   return companyProduct.ArtNo;    
 }  

Step 5

Now, open the product.service.ts file and add the following code,

import { Injectable } from '@angular/core';  
  
@Injectable()  
  
export class ProductsService {  
  employees: any[]; 
  
  constructor() {  
  }  
  
  getAllProductsUsingTrackBy() {  
    return this.employees = [  
      {  
        ProductId: 1,  
        ArtNo: "100",  
        Provider: "OppoProvider",  
        ProviderArtNo: "1Yu",  
        Brand: "Oppo",  
        Price: 7810.23,  
        BuyAccount: "123",  
        SalesAccount: "321"  
      },  
      {  
        ProductId: 1,  
        ArtNo: "101",  
        Provider: "OppoProvider",  
        ProviderArtNo: "1Yu",  
        Brand: "Oppo",  
        Price: 2310.23,  
        BuyAccount: "123",  
        SalesAccount: "321"  
      },  
      {  
        ProductId: 1,  
        ArtNo: "102",  
        Provider: "OppoProvider",  
        ProviderArtNo: "1Yu",  
        Brand: "Oppo",  
        Price: 7810.23,  
        BuyAccount: "123",  
        SalesAccount: "321"  
      },  
      {  
        ProductId: 1,  
        ArtNo: "103",  
        Provider: "OppoProvider",  
        ProviderArtNo: "1Yu",  
        Brand: "Oppo",  
        Price: 5810.23,  
        BuyAccount: "123",  
        SalesAccount: "321"  
      }  
    ];  
  }  
  
  getAllProductsUsingTrackByExample() {  
    return this.employees = [  
      {  
        ProductId: 1,  
        ArtNo: "100",  
        Provider: "OppoProvider",  
        ProviderArtNo: "1Yu",  
        Brand: "Oppo",  
        Price: 7810.23,  
        BuyAccount: "123",  
        SalesAccount: "321"  
      },  
      {  
        ProductId: 1,  
        ArtNo: "101",  
        Provider: "OppoProvider",  
        ProviderArtNo: "1Yu",  
        Brand: "Oppo",  
        Price: 2310.23,  
        BuyAccount: "123",  
        SalesAccount: "321"  
      },  
      {  
        ProductId: 1,  
        ArtNo: "102",  
        Provider: "OppoProvider",  
        ProviderArtNo: "1Yu",  
        Brand: "Oppo",  
        Price: 7810.23,  
        BuyAccount: "123",  
        SalesAccount: "321"  
      },  
      {  
        ProductId: 1,  
        ArtNo: "103",  
        Provider: "OppoProvider",  
        ProviderArtNo: "1Yu",  
        Brand: "Oppo",  
        Price: 5810.23,  
        BuyAccount: "123",  
        SalesAccount: "321"  
      },  
      {  
        ProductId: 1,  
        ArtNo: "104",  
        Provider: "OppoProvider",  
        ProviderArtNo: "1Yu",  
        Brand: "Oppo",  
        Price: 4770.23,  
        BuyAccount: "143",  
        SalesAccount: "211"  
      },  
    ];  
  }  
  
}  

Now it’s time to run the project by using ‘npm start’ or ‘ng serve’ command and check the output.

As in the below image when we click on “New Companies” it will add one new entry in the table .

This is image title

If we use normal *ngFor directive without using trackBy function Angular application will remove all the DOM elements and will recreate them again in the DOM tree, even if the same data is coming. This can slow down our application performance if the data is large in number.

This is image title

But with the help of trackBy it will not create a new DOM as we can see in the below image. It just gets added only the new data in the DOM which will increase the application performance.

This is image title

Conclusion

In this article, we have seen how to use trackBy with NgFor in Angular 8 Application.

Please give your valuable feedback/comments/questions about this article. Please let me know if you liked and understood this article and how I could improve it.

Thank you!

#angular #NgFor in Angular 8 #programming