【问题标题】:Cannot find control with path error in for formArray in angular在 Angular 中找不到路径错误的控件
【发布时间】:2021-10-31 23:19:15
【问题描述】:

component.ts:

 ngOnInit(): void {
 this.getVgAuditDetails();
 this.vgaReportForm = this.formBuilder.group({
  section_id: [this.sectionId],
  userId: [this.userObj.id],
  QuestionId: [this.questionId],
  answerDetails: this.formBuilder.array([]),
  vgaAuditNumber: [this.vgAudit.vga_audit_number],
  clientId: [this.clientId]
});
}
newAnswerDetails(): FormGroup {
return this.formBuilder.group({
  core: [''],
  vitalInterests: [''],
  bufferZones: [''],
  pivotalZones: [''],
  forwardPositions: [''],
  powerVaccums: [''],
  textbox1: [''],
  textbox2: [''],
  textbox3: [''],
  textbox4: [''],
  txtIntiative1: [''],
  txtIntiative2: [''],
  txtIntiative3: [''],
  txtOperatingProperties1: [''],
  txtOperatingProperties2: [''],
  txtOperatingProperties3: [''],
  txtOperatingProperties4: [''],
  txtOperatingProperties5: [''],
  txtOperatingProperties6: [''],
});
}

addAnswerDetails(): void {
 this.answerDetails = this.vgaReportForm.get('answerDetails') as FormArray;
 this.answerDetails.push(this.newAnswerDetails());
}

onSubmit(){
 console.log(this.vgaReportForm.value);
}

compoent.html:

<form action="" [formGroup]="vgaReportForm" (ngSubmit)="onSubmit()">
<div formArrayName="answerDetails">
  <!-- <div *ngFor="let vg of AnswerDetails.controls; let i=index"> -->
    <div [formGroupName]="i">
      <table class="table table-striped table-bordered table-hover dataTable">
        <thead>
          <tr class="thead">
            <th style="width: 25%">Zone of the Sphere</th>
            <th style="width: 25%">Strategic Intent</th>
          </tr>
          <tr>
            <td>Core</td>
            <td> <input type="text" class="form-control" formControlName="core"> </td>
          </tr>
          <tr>
            <td>Vital Interests</td>
            <td> <input type="text" class="form-control" formControlName="vitalInterests"> </td>
          </tr>
          <tr>
            <td>Buffer Zones</td>
            <td> <input type="text" class="form-control" formControlName="bufferZones"> </td>
          </tr>
          <tr>
            <td>Pivotal Zones</td>
            <td> <input type="text" class="form-control" formControlName="pivotalZones"> </td>
          </tr>
          <tr>
            <td>Forward Positions</td>
            <td> <input type="text" class="form-control" formControlName="forwardPositions"> </td>
          </tr>
          <tr>
            <td>Power Vacuums</td>
            <td> <input type="text" class="form-control" formControlName="powerVaccums"> </td>
          </tr>
        </thead>
    </table>
 </div>
 </div>
 </form>

当我运行这段代码时,它会抛出一个错误,例如,

找不到控件 路径:'answerDetails -> -> core',找不到带有路径的控件: 'answerDetails -> -> vitalInterests',找不到带有路径的控件: 'answerDetails -> -> forwardPositions' 等等

请帮我找到解决办法。

【问题讨论】:

  • 您正在使用 i 但此行已被注释。取消注释不能解决问题?

标签: angular formarray


【解决方案1】:

你应该在 html 中使用这段代码,也许它会有所帮助

formArrayName="AnswerDetails" *ngFor="let item of fg.get('AnswerDetails')['controls']; 

【讨论】:

  • 你能不能给我一些截图或任何东西,以便我能帮忙
猜你喜欢
  • 2017-12-09
  • 2021-09-09
  • 2020-08-18
  • 1970-01-01
  • 2020-03-03
  • 2019-08-09
  • 2023-03-07
  • 2019-12-11
  • 2018-12-24
相关资源
最近更新 更多