1

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.

14
  • 1
    Try if (name) instead of if (name.length > 0). Commented Jun 8, 2018 at 18:52
  • 1
    element.name is undefined in template. change it to element?name Commented Jun 8, 2018 at 18:58
  • 1
    @ConnorsFan suggested trick should work and your error should not be same. OR you can try name?.length I am not sure that works in the .ts or not Commented Jun 8, 2018 at 18:58
  • 1
    You have not assigned any value to <mat-cell fxFlex="40%" mat-cell *matCellDef="let row; let element" element Commented Jun 8, 2018 at 19:00
  • 1
    element is assigned just above and your code is retrieving the properties from blank element OR you can just write a console.log(name) just above the if (name.length > 0) { statement then you will get what value is passed into it. Commented Jun 8, 2018 at 19:13

1 Answer 1

1

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'}">
Sign up to request clarification or add additional context in comments.

12 Comments

The error comes from checkIfTrue(), namely because element.name is undefined which doesn't have a length property
Yes. If you don't pass something you are checking for undefined.length. undefined doesn't have a length property. Check if (name && name.length) @profiler
@baao i know but even if i change it to element.?name it causes error because it's methods parameter.
The html is not connected to this error @fatemefazli
yes as @baao said if you check if (name && name.length) in your function it prevent error.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.