[英]reset reactive form Angular Material
I am trying to reset my form after sending the data. 发送数据后,我试图重置我的表格。 I've almost done it except for the detail of the validation of the mail format.
除了验证邮件格式的细节外,我几乎已经完成了。 I should mention that
this.form.reset()
does not work for me. 我应该提到
this.form.reset()
对我不起作用。
Any idea how to fix it? 知道如何解决吗?
<form [formGroup]="forma" (ngSubmit)="agregarDomiciliario()" novalidate> <mat-form-field> <input matInput placeholder="Valor de la propiedad" formControlName="valorPropiedad"> <mat-error *ngIf="forma.controls['valorPropiedad'].invalid">{{ errorValorPropiedad() }}</mat-error> </mat-form-field> <div class="row"> <mat-form-field class="col-md-6"> <input matInput placeholder="Ingresa tu email" formControlName="email"> <mat-error *ngIf="forma.controls['email'].invalid">{{ errorEmail() }}</mat-error> </mat-form-field> <mat-form-field class="col-md-6"> <input matInput placeholder="Ingresa tu celular" formControlName="celular"> <mat-error *ngIf="forma.controls['celular'].invalid">{{ errorCelular() }}</mat-error> </mat-form-field> </div> <div class="row mt-4"> <div class="col-md-6"> <mat-checkbox class="small" formControlName="acepta">Acepto los términos y condiciones</mat-checkbox> </div> <div class="col-md-6 text-right"> <button mat-raised-button color="primary" type="submit" [disabled]="!forma.valid">Cotizar <i class="fas fa-arrow-right ml-2"></i></button> </div> </div> </form>
forma: FormGroup; constructor(fb: FormBuilder) { this.forma = fb.group ({ valorPropiedad: [ '', Validators.required ], email : ['', [Validators.required, Validators.email] ], celular: [ '', Validators.required ], acepta : [ false, Validators.requiredTrue ] }); } agregarDomiciliario() { console.log(this.forma.value); this.forma.setValue({ valorPropiedad : [''], email: [''], celular: [''], acepta : false, }); }
Your method invoked on the (ngSubmit)
event dispatches an invalid setValue
call. 在
(ngSubmit)
事件上调用的方法将分派无效的setValue
调用。 If you want your form to match the values defined in your constructor it should look like this: 如果希望表单与构造函数中定义的值匹配,则它应如下所示:
agregarDomiciliario() {
this.forma.setValue({
valorPropiedad : '',
email: '',
celular: '',
acepta : false,
});
}
Otherwise you set the valorPropiedad
, email
and celular
values to ['']
(array with a single empty string) and not ''
(empty string). 否则,请将
valorPropiedad
, email
和celular
值设置为['']
(带有单个空字符串的数组),而不是''
(空字符串)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.