Angular 如何获取嵌套表单组控件
我已经创建了如下所示的反应形式Angular 如何获取嵌套表单组控件,angular,angular-reactive-forms,Angular,Angular Reactive Forms,我已经创建了如下所示的反应形式 this.customerForm = this.fb.group({ "name": ["", Validators.required], "email": "", "info": this.fb.group({ "name": ["", Validators.required], }) }) get f() { return this.customerForm.controls } &
this.customerForm = this.fb.group({
"name": ["", Validators.required],
"email": "",
"info": this.fb.group({
"name": ["", Validators.required],
})
})
get f() { return this.customerForm.controls }
<p *ngIf="f.name.errors?.required">Required</p>
get fi() { return this.customerForm.controls.info.controls }
我添加了获取表单控件的getter函数,如下所示
this.customerForm = this.fb.group({
"name": ["", Validators.required],
"email": "",
"info": this.fb.group({
"name": ["", Validators.required],
})
})
get f() { return this.customerForm.controls }
<p *ngIf="f.name.errors?.required">Required</p>
get fi() { return this.customerForm.controls.info.controls }
我正在显示错误消息,如下所示
this.customerForm = this.fb.group({
"name": ["", Validators.required],
"email": "",
"info": this.fb.group({
"name": ["", Validators.required],
})
})
get f() { return this.customerForm.controls }
<p *ngIf="f.name.errors?.required">Required</p>
get fi() { return this.customerForm.controls.info.controls }
但它不起作用,我也不知道为什么它不起作用。有人能帮忙解决这个问题吗?提前感谢。关于formGroup对象。errors属性不包含任何内容,并且将始终返回null。在组级别,您只能判断它是否有效。对于实际错误,您需要进入嵌套控件
this.customerForm.get('info').valid // would work
this.customerForm.get('info').invalid // would work
this.customerForm.get('info').errors // will return null even if the control are in error
this.customerForm.get('info.name').errors // would work
控件是数组而不是对象。执行此操作
this.customerFrom.get('info')。控件是否收到任何错误?是。我得到了控制undefined@ibenjelloun. 不工作。你在用chrome 80吗?