简体   繁体   English

Angular。 错误无法读取 null 的属性(读取“addFormGroup”)

[英]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.

相关问题 angular 错误类型错误:无法读取 null 的属性(读取“_rawValidators”) - angular ERROR TypeError: Cannot read properties of null (reading '_rawValidators') Angular 错误:无法读取 null 的属性(读取“控件”) - Angular Error: Cannot read properties of null (reading 'controls') Angular:无法读取 null 的属性(读取“cannotContainSpace”) - Angular: Cannot read properties of null (reading 'cannotContainSpace') 无法读取未定义的属性“ next”。 有角度的。 重命名错误 - Cannot read property 'next' of undefined. Angular. Error on renaming 角度测试未捕获错误:未捕获(承诺):类型错误:无法读取 null 的属性(读取“参数”) - Angular Testing Uncaught Error: Uncaught (in promise): TypeError: Cannot read properties of null (reading 'params') 如何通过热重载在本地使用 angular 库? [错误:无法读取 null 的属性(读取“firstCreatePass”)] - How to use angular library locally with hot reload? [Error: Cannot read properties of null (reading 'firstCreatePass')] TypeError:无法读取 null 的属性(读取“scrollHeight”)| Angular 11 - TypeError: Cannot read properties of null (reading 'scrollHeight') | Angular 11 无法以 Angular 形式的嵌套形式读取 null 的属性(读取“控件”) - Cannot read properties of null (reading 'controls') in Nested Form of Angular Form Angular 12 - 类型错误:无法读取 null 的属性(读取“writeValue”) - Angular 12 - TypeError: Cannot read properties of null (reading 'writeValue') 无法读取 null 的属性(读取“nativeElement”)- 测试 Angular - Cannot read properties of null (reading 'nativeElement') - Test Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM