Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - trackBy with array of primitives

Tags:

angular

I've read a documentation, I've tried to google it and check with code but still I can't find an answer.

I have an array of string, for example ['Tom', 'Sarah', 'Ben']. Its content will change dynamically and regularly. I'd like to use a trackBy function with it.

  1. Generally, is there sense to do it?
  2. If so, how should I use trackBy with such array? I want to avoid converting data to object with id.

Thank you

like image 547
Roghul Avatar asked May 09 '26 23:05

Roghul


1 Answers

A typical way is to use index.

html

<div *ngFor="let e of array; trackBy: trackByFunc;">
   ...
</div>

ts

trackByFunc = (index: number, value: string) => index;
like image 56
N.F. Avatar answered May 12 '26 12:05

N.F.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!