Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I need to give a class to a <tr> if a property of item has the same value as a property from an object in an array.

Here's the code I currently have:

<tr *ngFor="let item of closingDayGroupsList" [class.inactive]="definitionDetails.Groups.filter(i => i.AbsenceReservationGroupID === item.ID).length > 0">

however now I receive the error:

Bindings cannot contain assignments

I'm not sure if what I'm doing is bad practice, or if I'm just making syntax errors.

This is the only way I know to achieve what I want, but it's not working

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
833 views
Welcome To Ask or Share your Answers For Others

1 Answer

Its a bad practice to use expressions in angular bindings

Move the class expression into controller.

export class AppComponent {
    title = 'Groups';

    getClass(item): void {

      // add filter logic here
      return this.definitionDetails.Groups.filter(i => i.AbsenceReservationGroupID === item.ID).length > 0

    }
}

The tr will be something like,

<tr *ngFor="let item of closingDayGroupsList" [class.inactive]="getClass(item)">

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...