简体   繁体   中英

Angular - trackBy with array of primitives

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

A typical way is to use index.

html

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

ts

trackByFunc = (index: number, value: string) => index;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM