[英]Angular 5 - reactive form- is there a way to patch value using a variable?
I have a form group with many fields : 我有一个包含许多字段的表单组:
this.addressForm = this.formBuilder.group({
line1: ['', Validators.required],
line2: '',
line3: '',
line4: '',
line5: '',
line6: '',
line7: '',
line8: '',
});
In my html I have a form field to each formControl and a button near it that clears that form control. 在我的html中,我对每个formControl都有一个表单字段,并在其附近有一个按钮可以清除该表单控件。
<mat-form-field>
<mat-label>line 1</mat-label>
<input matInput formControlName="line1" type="text">
<button type="button" (click)="clearLine('line1')">
</button>
</mat-form-field>
How Can I write a generic methods that gets the name of the form control and clears it? 如何编写一个通用方法来获取表单控件的名称并清除它?
I tries this- 我尝试这个
clearLine(line) {
this.addressForm.patchValue({line: ''});
}
but that did not work, because it searched for a formControl name "line". 但这不起作用,因为它搜索了一个formControl名称“ line”。
Is there any way to do this without execute many "if" conditions? 有没有执行许多“如果”条件就可以做到这一点的方法吗?
try this 尝试这个
clearLine(line) {
this.addressForm.patchValue({[line]: ''}); // added []
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.