【发布时间】:2019-06-24 02:48:49
【问题描述】:
我正在使用 Angular 7
我有一个嵌套的响应式表单
this.salonProfileForm = this.fb.group({
salonName: new FormControl('', [Validators.required]),
address: this.fb.group({
city: new FormControl('', [Validators.required])
})
});
get f() {
return this.salonProfileForm.controls;
}
我有类似的 HTML 表单
<input type="text" formControlName="salonName" required />
<ng-container *ngIf="submitted && f.salonName.invalid && (f.salonName.dirty || f.salonName.touched)">
<small *ngIf="f.salonName.errors.required">
Salon name is required
</small>
</ng-container>
<div formGroupName="address">
<input type="text" formControlName="city" />
<ng-container *ngIf="submitted && f.city.invalid && (f.city.dirty || f.city.touched)">
<small *ngIf="f.city.errors.required">
city is required
</small>
</ng-container>
</div>
但这会导致城市输入 ng-container 字段出现错误
ERROR TypeError: Cannot read property 'invalid' of undefined
如何验证嵌套输入字段?
console.log(this.f.address)
【问题讨论】:
标签: angular angular-reactive-forms