我想禁用“addressLine1”验证器;我怎么能禁用它?看到它嵌套在“地址”元素
中 this.form = fb.group({
'region': [null, Validators.required],
'address': fb.group({
'addressLine1': ["", Validators.required],
'addressLine2': [""],
'city': ["", Validators.required]
}),
})
我在下面尝试了这个但是没有用
this.form.controls["address"]["addressLine1"] .disable();
谢谢
这是html:
<form [formGroup]="form">
<div ><label><span class="required">*</span>State/Province/Region<br><input class="form-control" pInputText formControlName="addressLine1" [(ngModel)]="selected.address.addressLine1" required></label></div>
</form>
答案 0 :(得分:5)
正确的语法应该是:
(this.form.controls["address"] as FormGroup).controls["addressLine1"].disable({});
甚至更好
this.form.get('address.addressLine1').disable();
<强> Stackblitz example 强>
<强>更新强>
要禁用验证器,请使用以下代码:
const control = this.form.get('address.addressLine1');
control.setValidators(null);
control.updateValueAndValidity();
<强> Stackblitz Example 强>
答案 1 :(得分:1)