Still I got below error: Type Error Cannot read property 'length', is connected to html line 9:
Test
</mat-header-cell>
<mat-cell fxFlex="40%" mat-cell *matCellDef="let row; let element"
line 9: [ngStyle]="checkIfTrue(element.name) && {'background-color':'lightgreen'}">
<mat-checkbox [ngStyle]="checkIfTrue(element.name) && {'background-color':'white'}" (click)="$event.stopPropagation()" (change)="$event ? selection.toggle(row) : null" [checked]="selection.isSelected(row)">
</mat-checkbox>
</mat-cell>
</ng-container>
And checkIfTrue from component:
checkIfTrue(name?: string) {
if (name) {
if (!this.isLoadingArray[this.groupName]) {
for (const team of this.teams) {
if (name === team.teamPromotion1 || name === team.teamPromotion2) {
return true;
} else {
return false;
}
}
}
} else {
return false;
}
}
I think, all the values were initialized.
if (name)instead ofif (name.length > 0).element.nameis undefined in template. change it toelement?namename?.lengthI am not sure that works in the.tsor not<mat-cell fxFlex="40%" mat-cell *matCellDef="let row; let element"elementelementis assigned just above and your code is retrieving the properties from blank element OR you can just write aconsole.log(name)just above theif (name.length > 0) {statement then you will get what value is passed into it.