#can I add immediately invoked function to [trackBy]?

7 messages · Page 1 of 1 (latest)

left anvil
#

As you see the code below, this is a normal example. As seen TrackBy accepts as a function (what do you call that? predicate?)

then probably I can just immediately invoke a function like the example below in one line but I haven't seen that example. How can I make trackby using one line in html
and what's the best practice of using trackBy? it's always best recommended using trackBy in every loop?

<table cdk-table [dataSource]="dataSource" [trackBy]="myTrackById">

myTrackById(index: number, item: any){
  return item.id;
}

<table cdk-table [dataSource]="dataSource" [trackBy]="(index, item)=>item.id">
 

slender musk
#

AFAIK no. you cannot instantiate functions in an Angular template.

#

But you have to code there, did you try it? Did it work?

left anvil
#

I have tried but it doesn't work :). Do you use trackby in every loop or it's overkilling sometime?

slender musk
#

I use trackBy in any loop over Objects. It's 1 line of code, i see no reason not to.

#

But I did make a pipe that i use to create trackByProperty methods