Skip to content

Instantly share code, notes, and snippets.

@gesielrosa
Last active July 4, 2022 18:08
Show Gist options
  • Save gesielrosa/c8e846be9879e580487923a5cc803bc9 to your computer and use it in GitHub Desktop.
Save gesielrosa/c8e846be9879e580487923a5cc803bc9 to your computer and use it in GitHub Desktop.
Track by pipe for Angular
import {NgModule, Pipe, PipeTransform} from '@angular/core';
@Pipe({
name: 'trackByKey',
pure: true,
})
export class TrackByKeyPipe implements PipeTransform {
public transform(key: string) {
return (index, value) => {
return value[key];
};
}
}
@NgModule({
declarations: [TrackByKeyPipe],
exports: [TrackByKeyPipe],
})
export class TrackByKeyPipeModule {}
@gesielrosa
Copy link
Author

gesielrosa commented Jul 4, 2022

Usage:

<ng-container *ngFor="let user of users; trackBy: 'id' | trackByKey">
  <!-- code -->
</ng-container>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment