【发布时间】:2019-07-10 19:59:51
【问题描述】:
我无法绑定来自 FormGroup 的输入之一:
<input matInput placeholder="Center" value="Manila" formControlName="location" disabled>
它也没有被禁用。
当我插入[(ngModel)]="location" 以获取该值时,据说它已被删除或弃用。但输入被禁用。
我的表单(sn-p):
<mat-form-field class="tribe-full-width">
<input matInput placeholder="Tribe Name" value="" formControlName="tribeName">
</mat-form-field>
<mat-form-field class="tribe-full-width">
<input matInput placeholder="Tribe Leader Name" value="" formControlName="tribeLeader">
</mat-form-field>
<mat-form-field class="tribe-full-width">
<input matInput placeholder="Center" value="Manila" formControlName="location">
</mat-form-field>
我的组件:
onSubmitTribeData() {
console.log(this.tribeForm.value);
}
addTribe(){
if (this.showForm === false) {
this.showForm = true;
this.tribeForm.controls['dtcLocation'].disable();
this.tribeForm = this.fb.group({
tribeName: [''],
tribeLeader: [''],
location: [''],
tribeSquad: ['']
});
} else {
this.showForm = false;
}
}
【问题讨论】:
-
你能在 stackblitz sn-p 中演示这个问题吗?
-
显示值使用 myForm.getRawValue(),而不是 myForm.value:angular.io/api/forms/FormGroup#getRawValue,给出值使用 myForm.get('location').setValue(),并且永远不要一起使用[ngModel] 和 formControlName 在属于 formGroup 的变量上
标签: angular angular-reactive-forms formgroups