首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在安圭拉用嵌套的FormArray访问FormArray?

在安圭拉用嵌套的FormArray访问FormArray?
EN

Stack Overflow用户
提问于 2022-09-25 06:54:05
回答 1查看 28关注 0票数 0

在另一个FormArray ho中拥有一个FormArray,我可以访问第二个FormArray吗?

这就是我在我的组成部分中所拥有的:

代码语言:javascript
复制
registrationForm = new FormGroup({
    registrations: new FormArray([this.patchRegistrationValues()])
  });

patchRegistrationValues(): FormGroup {
    return new FormGroup({
      //some other FormControls
      setDate: new FormArray([this.patchSetDate()]),
    });
  }

get registrations(): FormArray {
    return this.registrationForm.get('registrations') as FormArray;
  }

如何访问setDate来填充它?我试过了,但没有用:

代码语言:javascript
复制
get setDate(): FormArray {
    return this.registrations.get('setDate') as FormArray;
  }

现在有人怎么做吗?我真的需要索引吗?

更新

关于最近的回复,这是我的html:

代码语言:javascript
复制
<thead>
    <tr>
        <!-- TODO: add translation to table -->
        <th>Project</th>
        <th>Date</th>
        <th>Buttons</th>
        <th nzAlign="center" class="fit">
            <app-add-btn (click)="addRegistrationRow()" label="{{'common.add' | translate}}"></app-add-btn>
        </th>
    </tr>
</thead>
<tbody formArrayName="registrations">
    <tr *ngFor="let reg of registrations.controls; let i = index" [formGroupName]="i">
        <td>
            <!--FormControl goes here like an input-->
        </td>
        <td>
            <ng-container *ngFor="let date of setDate.controls let ind = index" [formGroupName]="ind">
                <!--Not so shure about this part, any input i set here doesn't show in the page is like a blank cellulare-->

            </ng-container>
        </td>
        <td nzAlign="center" class="fit">
            <app-delete-btn (click)="removeRegistrationRow(i)"></app-delete-btn>
            <app-add-btn (click)="addSetDateRow()" label="{{'common.add' | translate}}"></app-add-btn> <!-- this doesn't work -->
        </td>
    </tr>
</tbody>

再次为迟到感到抱歉!

EN

回答 1

Stack Overflow用户

发布于 2022-09-25 07:37:45

您需要向父FormArray提供索引。

代码语言:javascript
复制
getSetDateControlFromRegistrationForm(registrationIndex: number): FormArray {
  return (this.registrations.controls[registrationIndex] as FormGroup)
    .get('setDate') as FormArray;
}

怀疑通过提供索引作为参数,不能将该方法作为getter应用。

更新

基于附加的HTML模板,所以您需要的是:

  1. formArrayName="setDate"的包装器
  2. 使用上面提供的方法迭代setDate FormArray控件。
  3. 大部分内部部分将是FormGroup及其FormControl(s)。
代码语言:javascript
复制
<ng-container formArrayName="setDate">
  <ng-container
    *ngFor="
      let date of getSetDateControlFromRegistrationForm(i).controls;
      let ind = index
    "
  >
    <!-- Form Group in each setDate Form Array -->
    <div [formGroupName]="ind">
      <input formControlName="i" />

      <input formControlName="date" />
    </div>
  </ng-container>
</ng-container>

Demo @ StackBlitz

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73842587

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档