ProductPromotion
Logo

Angular.JS

made by https://0x3d.site

GitHub - nigrosimone/ng-for-track-by-property: Angular global trackBy property directive with strict type checking.
Angular global trackBy property directive with strict type checking. - nigrosimone/ng-for-track-by-property
Visit Site

GitHub - nigrosimone/ng-for-track-by-property: Angular global trackBy property directive with strict type checking.

GitHub - nigrosimone/ng-for-track-by-property: Angular global trackBy property directive with strict type checking.

NgForTrackByProperty Build Status Coverage Status NPM version

Angular global trackBy property directive with strict type checking.

Description

When displaying a list of data (at least somewhat large lists) you should be using Angular's trackBy feature which looks something like:

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

interface Item { 
  id: number; 
  name: string;
}

@Component({
  selector: 'app-root',
  template: `
    <ul>
      <li *ngFor="let item of list; trackBy: trackById">
        {{ item.id }} {{ item.name }}
      </li>
    </ul>
  `,
})
export class AppListComponent {
  public list: Array<Item> = [
    { id: 0, name: 'foo' },
    { id: 1, name: 'bar' },
    { id: 2, name: 'baz' },
  ];

  public trackById(index: number, item: Item) {
    return item.id;
  }
}

Unfortunately, Angular forces us to write a tracking function in each component in which we want to make use of trackBy. With ng-for-track-by-property you could just handle this entirely in the template by passing a property like this:

import { Component } from '@angular/core';
import { NgForTrackByPropertyDirective } from 'ng-for-track-by-property';

interface Item { 
  id: number; 
  name: string;
}

@Component({
  selector: 'app-root',
  imports: [NgForTrackByPropertyDirective],
  template: `
    <ul>
      <li *ngFor="let item of list; trackByProperty: 'id'">
        {{ item.id }} {{ item.name }}
      </li>
    </ul>
  `,
})
export class AppListComponent {
  public list: Array<Item> = [
    { id: 0, name: 'foo' },
    { id: 1, name: 'bar' },
    { id: 2, name: 'baz' },
  ];
}

ng-for-track-by-property has strict type checking and only available property are allowed

alt text

See the stackblitz demo.

Features

✅ trackBy property name ✅ Type casting ✅ trackBy index

Get Started

Step 1: install ng-for-track-by-property

npm i ng-for-track-by-property

Step 2: add trackByProperty to your ngFor, eg.:

import { Component } from '@angular/core';
import { NgForTrackByPropertyDirective } from 'ng-for-track-by-property';

interface Item { 
  id: number; 
  name: string;
}

@Component({
  selector: 'app-root',
  imports: [NgForTrackByPropertyDirective],
  template: `
    <ul>
      <li *ngFor="let item of list; trackByProperty: 'id'">
        {{ item.id }} {{ item.name }}
      </li>
    </ul>
  `,
})
export class AppComponent {
  public list: Array<Item> = [
    { id: 0, name: 'foo' },
    { id: 1, name: 'bar' },
    { id: 2, name: 'baz' },
  ];
}

you can also track by index with trackByIndex, eg.:

import { Component } from '@angular/core';
import { NgForTrackByIndexDirective } from 'ng-for-track-by-property';

interface Item { 
  id: number; 
  name: string;
}

@Component({
  selector: 'app-root',
  imports: [NgForTrackByIndexDirective],
  template: `
    <ul>
      <li *ngFor="let item of list; trackByIndex">
        {{ item.id }} {{ item.name }}
      </li>
    </ul>
  `,
})
export class AppComponent {
  public list: Array<Item> = [
    { id: 0, name: 'foo' },
    { id: 1, name: 'bar' },
    { id: 2, name: 'baz' },
  ];
}

since track by property id is a very common case, there is also trackById:

import { Component } from '@angular/core';
import { NgForTrackByIdDirective } from 'ng-for-track-by-property';

interface Item { 
  id: number; 
  name: string;
}

@Component({
  selector: 'app-root',
  imports: [NgForTrackByIdDirective],
  template: `
    <ul>
      <li *ngFor="let item of list; trackById">
        {{ item.id }} {{ item.name }}
      </li>
    </ul>
  `,
})
export class AppComponent {
  public list: Array<Item> = [
    { id: 0, name: 'foo' },
    { id: 1, name: 'bar' },
    { id: 2, name: 'baz' },
  ];
}

Available directives

Directive HTML template syntax TrackBy function
trackByProperty: key *ngFor="let item of arr; trackByProperty: 'id'" (index, item) => item[key]
trackByIndex *ngFor="let item of arr; trackByIndex" (index, item) => index
trackById *ngFor="let item of arr; trackById" (index, item) => item.id

Support

This is an open-source project. Star this repository, if you like it, or even donate. Thank you so much!

My other libraries

I have published some other Angular libraries, take a look:

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory