【问题标题】:Error: Error trying to diff ''. Only arrays and iterables are allowed错误:尝试区分“”时出错。只允许使用数组和可迭代对象
【发布时间】:2021-07-01 10:47:56
【问题描述】:

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

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

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

错误:尝试比较“”时出错。只允许使用数组和可迭代对象 你能帮我找出解决办法吗

【问题讨论】:

  • [selected]="student.name[value]="student"。缺少 [selected]="student.name 的结束双引号?
  • 没有你的 ts 很难判断,但如果你使用表单,它们有一个内置的重置方法以及设置/修补表单值的方法。
  • studentList 是一个字符串。您不能使用 ngFor 遍历字符串,这就是错误消息试图告诉您的内容。在你的重置方法中尝试this.studentList = [];
  • 乐于助人。我再次发布它作为答案。随意接受它作为您问题的答案。

标签: angular typescript


【解决方案1】:

属性studentList 是一个字符串。

您不能使用*ngFor 遍历字符串,这就是错误消息试图告诉您的内容。欲了解更多信息,请查看Angular directive docs

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-09
    • 2018-09-14
    • 1970-01-01
    • 2021-07-23
    • 2019-10-26
    • 2020-08-15
    • 2021-06-06
    相关资源
    最近更新 更多