profiler
profiler

Reputation: 627

Type Error, Angular 5: Cannot read property 'length'

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.

Upvotes: 1

Views: 648

Answers (1)

Lia
Lia

Reputation: 11982

because element.name is not defined in html causes error and it's not work even if you change it to element?.name, you can do something like this:

<mat-cell *ngIf="element.name" fxFlex="40%" mat-cell *matCellDef="let row; let element"
[ngStyle]="checkIfTrue(element.name) && {'background-color':'lightgreen'}">

Upvotes: 1

Related Questions