使用Angular反应形式更新<input/>
值的最佳做法是什么?
我有2个formGroup。
首先,formGroup有一个blur
事件,它将更新第二个formGroup上的输入标记
<input
class="form-control"
id="bill_firstname"
type="text"
name="firstname"
formControlName="firstname"
autocomplete="off"
placeholder="Corporate"
(blur)="onBlurTest($event)"
/>
第二个formGroup:
<input
class="form-control"
id="doc-first-name"
type="text"
name="doctor"
formControlName="docFirstName"
autocomplete="off"
placeholder="First Name"
[(value)]="test"
/>
在我的.ts
文件中,我将其设置为这样。
test = '';
onBlurTest(e) {
this.test = e.target.value;
this.companySignUpForm.value.user.docFirstName = e.target.value;
}
有没有正确的方法呢?或者这可以接受吗?
谢谢。
要更改formControl的值,您需要使用formControl的API。一种选择:
onBlurTest(e) {
this.companySignUpForm.get('use.docFirstName').patchValue(e.target.value)
}