开发者问题收集

错误:尝试比较“”。仅允许数组和可迭代对象

2021-07-01
5688

我有一个多选字段。当我重置该字段中的所有值时,会发生错误

Html

<mat-form-field appearance="outline" fxFlex="100" >
   <mat-label>StudentList</mat-label>
      <mat-select multiple (selectionChange)="checkAllSelected(formData.student)"
            class=componentWidth
            [(ngModel)]="formData.student"
            [ngModelOptions]="{standalone: true}" >
        <mat-option *ngFor="let student of studentList" [selected]="student.name[value]="student">
               {{student.name}}
        </mat-option>
      </mat-select>
 </mat-form-field>

Ts 重置

resetAll() {
        this.formData.period = "";
        this.formData.meetingCode = "";
        this.formData.meetingName = "";
        this.formData.chosenDate = "";
        this.formData.academic = "";
        this.formData.student = "";
        this.formData.time_from = "";
        this.formData.time_to = "";
        this.meetingType = '';
        this.studentList = "";
        
    }

当我单击重置时,它会显示错误

Error: Error trying to diff ''. Only arrays and iterables are allowed Can you help me to find out a solution

1个回答

属性 studentList 是一个字符串。

您无法使用 *ngFor 迭代字符串,而这正是错误消息试图告诉您的内容。有关更多信息,请查看 Angular 指令文档

在您的重置方法中通过 this.studentList = []; 设置一个空数组。

Tino
2021-07-02