Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ANGULAR找不到路径为&x27;儿童形式阵列->;[对象]->;性别';_Angular_Typescript_Reactive Forms - Fatal编程技术网

ANGULAR找不到路径为&x27;儿童形式阵列->;[对象]->;性别';

ANGULAR找不到路径为&x27;儿童形式阵列->;[对象]->;性别';,angular,typescript,reactive-forms,Angular,Typescript,Reactive Forms,我正在做一个有角度的项目,我试图用反应式的形式动态地增加多个孩子的年龄和性别。虽然正在添加表单,但表单的删除部分不起作用。每次我尝试添加时,都会不断出现错误 Cannot find control with name: 'childrenFormArray' 这是我的ts代码 ngOnInit(): void { this.childrenFormGroup= this.formBuilder.group({ childrenFormArray: this.formBuild

我正在做一个有角度的项目,我试图用反应式的形式动态地增加多个孩子的年龄和性别。虽然正在添加表单,但表单的删除部分不起作用。每次我尝试添加时,都会不断出现错误

Cannot find control with name: 'childrenFormArray'
这是我的ts代码

 ngOnInit(): void {
   this.childrenFormGroup= this.formBuilder.group({
     childrenFormArray: this.formBuilder.array([ this.createItem() ])
   });
 }

 createItem(): FormGroup {
   return this.formBuilder.group({
     gender: new FormControl(''),
     age:  new FormControl(''),
  
   });
 }

 addItem(): void {

   if(this.childrenFormGroup.get('childrenFormArray'['controls'].length<this.numberOfChildren) {
       this.childrenFormArray = this.childrenFormGroup.get('childrenFormArray') as FormArray;
       this.childrenFormArray.push(this.createItem()); 
       this.addItem();
   }
   else if(this.childrenFormGroup.get('childrenFormArray'['controls']>this.numberOfChildren) {
     this.childrenFormArray = this.childrenFormGroup.get('childrenFormArray') as FormArray;
     this.childrenFormArray.removeAt(this.childrenFormArray.length-1);
     this.addItem()
 }
ngOnInit():void{
this.childrenFormGroup=this.formBuilder.group({
childrenFormArray:this.formBuilder.array([this.createItem()]))
});
}
createItem():FormGroup{
返回此.formBuilder.group({
性别:新FormControl(“”),
年龄:新FormControl(“”),
});
}
addItem():void{
if(this.childrenFormGroup.get('childrenFormArray'['controls'].lengthis.numberOfChildren){
this.childrenFormArray=this.childrenFormGroup.get('childrenFormArray')作为FormArray;
this.childrenFormArray.removeAt(this.childrenFormArray.length-1);
这是addItem()
}
}

这是我用来打印表单的html代码


儿童人数
男性
女性
其他

您需要确保html与表单匹配,例如表单具有

  childrenFormGroup: FormGroup
    childrenFormArray : FormArray
      [0] : FormGroup
          gender: FormControl
          age: FormControl
      [1] : FormGroup
          gender: FormControl
          age: FormControl

现在我们可以回到你的状态,试着在上面见面


儿童人数
男性
女性
其他
请注意这两行

<ng-container formArrayName="childrenFormArray">
...
 [formGroupName]='i'

...
[formGroupName]=“我”