Skip to content

Instantly share code, notes, and snippets.

@hakimio
Created August 10, 2022 18:46
Show Gist options
  • Select an option

  • Save hakimio/26c056ed64966bc51b845c23cbb6b9ad to your computer and use it in GitHub Desktop.

Select an option

Save hakimio/26c056ed64966bc51b845c23cbb6b9ad to your computer and use it in GitHub Desktop.
import {Directive, Host, Input} from '@angular/core';
import {NgForOf} from '@angular/common';
// Usage:
// *ngFor="let message of messages; trackById: 'created'"
@Directive({
selector: '[ngForTrackById]'
})
export class TrackByIdDirective<T> {
@Input('ngForTrackById')
property;
constructor(
@Host()
private ngFor: NgForOf<T>
) {
this.ngFor.ngForTrackBy = (index: number, item: T) => item[this.property];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment