FormArray.找不到路径控制 [英] FormArray. Cannot find control with path

查看:44
本文介绍了FormArray.找不到路径控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想输出一个字段,其中一个字段是一个数组,对于数组的每个元素,我都需要输出输入.

I want to output a form in which one field is an array, and for each element of the array I need to output my inputs.

组件:

ngOnInit() {
  const fb = this.fb;
  this.editForm = this.fb.group({
    country: fb.control(null),
    identifiers: this.fb.array([
       this.fb.group({
         codeIdentifier: fb.control(null),
         numIdentifier: fb.control(null),
       })
    ]),
 });
 this.organizationService.getOneById(this.id).subscribe((organization: Organization) => {
    let ids: any[] = [];
    organization.identifiers.forEach(item => {
       let id: any = { "codeIdentifier": "", "numIdentifier": "" };
       id.codeIdentifier = item.typeIdentifier.code;
       id.numIdentifier = item.numIdentifier;
       ids.push(id);
    });
     this.editForm.setControl('identifiers', this.fb.array(ids || []));
 })
}

HTML:

<div [formGroup]="editForm">
  <ng-container formArrayName="identifiers">
    <ng-container *ngFor="let identifier of identifiers.controls; let i=index" [formGroupName]="i">
      <input type="text" formControlName="codeIdentifier">
      <input type="text" formControlName="numIdentifier">
    </ng-container>
  </ng-container>
</div>

出现错误:

找不到具有以下路径的控件:标识符-> 0-> codeIdentifier"

Cannot find control with path: 'identifiers -> 0 -> codeIdentifier'

推荐答案

在构建时,请注意将其与 array 内的 group 进行设置,如下所示:

Pay attention to set it with the group inside the array as you built it, like that:

this.editForm.setControl('identifiers',
  this.fb.array(ids.map(
      id => this.fb.group({codeIdentifier: id})
    ) || [])
);

this.editForm.setControl('identifiers',
  this.fb.array(ids.map(
      id => this.fb.group({numIdentifier: id})
    ) || [])
);

  • 我不完全理解您的 Objects 结构,但这是将控制阵列构建回视图以供编辑/更新之用.
    • I didn't entirely understood your Objects structure, but this is the way to build the Control Array back to view for Edit/Update purposes.
    • 这篇关于FormArray.找不到路径控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆