【发布时间】:2020-05-20 02:45:56
【问题描述】:
我正在尝试在发送后重置我的表单,但只有值设置为 null。
component.html
<div *ngIf="!loading" fxLayout="row" class="note-textarea">
<form fxFlex fxLayout="column" fxLayoutGap="10px" [formGroup]="noteForm">
<mat-form-field fxFlex>
<textarea matInput #note rows="1" maxlength="100" placeholder="Note" formControlName="description"></textarea>
<mat-hint align="end">{{note.value?.length || 0}}/100</mat-hint>
<mat-error *ngIf="noteForm.get('description').errors && noteForm.get('description').touched">description is required</mat-error>
</mat-form-field>
<div fxFlex>
<button mat-stroked-button class="save-btn" (click)="insertNote()">Save</button>
</div>
</form>
</div>
component.ts
noteForm: FormGroup = this.formBuilder.group({
description: new FormControl(null, Validators.required)
})
insertNote() {
// bunch of code
this.noteForm.reset();
}
}
问题是输入字段被标记为脏,如下所示:
【问题讨论】:
-
@Ivilin Stouanov,了解如何使用
resetForm或type=reset。 -
reset 将通过标记所有后代标记为原始和未触及,并将所有后代的值重置为 null 来重置 FormGroup。 ??????????
标签: angular angular-reactive-forms reset reactive-forms