【发布时间】:2021-09-28 00:42:43
【问题描述】:
当我提交表单时,我在提交方法中这样重置它:
onSubmitPriceForm() {
// to do......
this.priceForm.reset();
}
}
我希望该字段恢复到照片 1 中的原始状态
我该怎么做?
谢谢!
在这里编辑验证器:
initPriceForm() {
this.priceForm = this.formBuilder.group({
price: ['', [Validators.required, Validators.pattern(this.pricePattern)]]
});
}
新编辑:这是 html 表单:
<form [formGroup]="priceForm" (ngSubmit)="onSubmitPriceForm()">
<div class="edit-price-form">
<mat-form-field appearance="outline" errorState="false">
<mat-label>{{ 'PRIX_TTC.NOUVEAU_PRIX_TTC' | translate }}</mat-label>
<input matInput name="price" autocomplete="off" formControlName="price" required>
</mat-form-field>
<button type="button" mat-mini-fab color="primary">
<mat-icon>done</mat-icon>
</button>
</div>
</form>
【问题讨论】:
-
很难说没有看到您的验证器实现,但通常您需要将您的 FormGroup / FormControls 标记为未触及和/或原始,即
this.priceForm.markAsPristine(); this.priceForm.markAsUntouched(); -
我添加了验证器,谢谢
标签: angular angular-material angular-reactive-forms mat-form-field