代码之家  ›  专栏  ›  技术社区  ›  profiler

类型错误,角度5:无法读取属性“length”

  •  2
  • profiler  · 技术社区  · 7 年前

    但我还是犯了以下错误: 类型错误无法读取属性“length”,该属性已连接到HTML第9行:

    试验

    </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> 
    

    从组件中检查结果:

       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;
        }
    
    }
    

    我想,所有的值都被初始化了。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Lia    7 年前

    因为element.name没有在html中定义,所以会导致错误,即使您将其更改为element,它也不会工作?名字,你可以这样做:

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