【发布时间】: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