[英]Angular. Error Cannot read properties of null (reading 'addFormGroup')
I have a component with a form:我有一个带有表单的组件:
<form [formGroup]="Nsfg.invoiceFormGroup">
<div formGroupName="namespace">
<div class="accordion" id="accordionTrikititta">
<newFacturaE></newFacturaE>
<fileHeaderComponent [invoiceFormGroup]="Nsfg.invoiceFormGroup">
</fileHeaderComponent>
...
As my form grows I need to split it in different components: First one 'fileHeaderComponent'随着表单的增长,我需要将其拆分为不同的组件:第一个“fileHeaderComponent”
In my child component I have in the Html:在我的子组件中,我在 Html 中有:
<div formGroupName="FileHeader">
<div class="form-inline">
....
then I receive an error:然后我收到一个错误:
ERROR TypeError: Cannot read properties of null (reading 'addFormGroup')错误类型错误:无法读取 null 的属性(读取“addFormGroup”)
And debugging it's happening because there is no parent found for the elements... where it should find the parent form it's null.并且正在调试它,因为没有找到元素的父级......它应该在哪里找到父级形式它是 null。
I've been trying some solutions found here (with no luck):我一直在尝试在这里找到一些解决方案(没有运气):
https://stackblitz.com/edit/angular-2b3zcz?file=src%2Fapp%2Faddress%2Faddress.component.html
https://stackblitz.com/edit/angular-ewdzmp?file=src%2Fapp%2Fcomponent%2Ffamily%2Ffamily.component.ts
Thanks to Jai Saravanan!感谢杰萨拉瓦南!
In my case I've fixed it this way:就我而言,我已经通过这种方式修复了它:
parent html (referring child):父 html(指子):
<form [formGroup]="Nsfg.invoiceFormGroup">
<div formGroupName="namespace">
<fileHeaderComponent formGroupName="FileHeader"></fileHeaderComponent>
html child: html 子:
<div [formGroup]="fileHeaderFormGroup">
ts child: ts 孩子:
export class FileHeaderComponent implements OnInit {
@Input() formGroupName: string;
ngOnInit(): void {
this.fileHeaderFormGroup = NsGet.fileHeader as FormGroup;
}
NsGet is equal to NsGet 等于
Nsfg.invoiceFormGroup.get('namespace').get('FileHeader');
You can refer to the below link for reactive form in the parent-child component in detail.您可以参考以下链接了解父子组件中的响应式表单。
https://blog.profanis.me/blog/reactive-forms-in-parent-child-components https://blog.profanis.me/blog/reactive-forms-in-parent-child-components
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.