![](/img/trans.png)
[英]Angular 5 reactive form- updating formControl through the html does not update the value, only the control
[英]Angular 5 - reactive form- is there a way to patch value using a variable?
我有一個包含許多字段的表單組:
this.addressForm = this.formBuilder.group({
line1: ['', Validators.required],
line2: '',
line3: '',
line4: '',
line5: '',
line6: '',
line7: '',
line8: '',
});
在我的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>
如何編寫一個通用方法來獲取表單控件的名稱並清除它?
我嘗試這個
clearLine(line) {
this.addressForm.patchValue({line: ''});
}
但這不起作用,因為它搜索了一個formControl名稱“ line”。
有沒有執行許多“如果”條件就可以做到這一點的方法嗎?
嘗試這個
clearLine(line) {
this.addressForm.patchValue({[line]: ''}); // added []
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.