简体   繁体   English

如何在 Angular 8 中正确使用表单数组/动态生成的表单与反应式表单

[英]How does one correctly use a formarray/dynamic generated form in conjunction with reactive forms in Angular 8

I'm relatively new to Angular and I am having an issue with a reactive form/form builder/form array.我对 Angular 比较陌生,我遇到了反应式表单/表单构建器/表单数组的问题。

Currently, I'm retrieving a list of companies from our API.目前,我正在从我们的 API 中检索公司列表。 I need to add an input field to add a 'special code' for each company before posting this back.在发回之前,我需要添加一个输入字段来为每个公司添加一个“特殊代码”。

The issue is that (and examples seem to have led me to this) I am now looping in a loop in order to get the contents of my form control array.问题是(并且示例似乎使我想到了这一点)我现在正在循环中以获取表单控件数组的内容。 Without this companyArray , I am getting errors because Angular is unable to find the form control.如果没有这个companyArray ,我会收到错误,因为 Angular 无法找到表单控件。 'form control can not be found -> 0' etc '找不到表单控件 -> 0' 等

I have put in a small hack in the HTML but I am not happy with this at all.我在 HTML 中做了一个小改动,但我对此一点也不满意。 I presume the solution is probably quite straightforward and I am doing something fundamentally silly.我认为解决方案可能非常简单,而且我正在做一些根本上很愚蠢的事情。 I am just missing the required knowledge and everything I try seems to break my example.我只是缺少所需的知识,我尝试的一切似乎都打破了我的榜样。 Any help would be most appreciated.非常感激任何的帮助。

I have created this simplified StackBlitz example below.我在下面创建了这个简化的 StackBlitz 示例。

StackBlitz EXAMPLE : https://stackblitz.com/edit/angular-267xnc StackBlitz 示例https : //stackblitz.com/edit/angular-267xnc

export class AppComponent {

  listOfPossibleCompaniesFromAPI: any[] = ['BBC', 'Apple', 'Amazon'];
  accountForm: FormGroup;

  ngOnInit() {
    this.accountForm = this.fb.group({
      'CompanyArray': this.fb.array([])
    });
    this.listOfPossibleCompaniesFromAPI.forEach(() => {
      this.CompanyArray.push(this.fb.group(new Group(false, '')));
    });
  }

  constructor(private fb: FormBuilder) {}

  get CompanyArray(): FormArray {
    return this.accountForm.get('CompanyArray') as FormArray;
  }
}
<form [formGroup]="accountForm">
  <div *ngFor="let companyName of listOfPossibleCompaniesFromAPI; let z=index">
    <div formArrayName="CompanyArray">
      <div *ngFor="let student of CompanyArray.controls; let i=index" [formGroupName]="z">
        <div *ngIf="z === i">
          <input type="checkbox" formControlName="selected"> {{ companyName }}
          <input formControlName="specialCode" placeholder="Enter special code">
        </div>
      </div>
    </div>
  </div </form>

  {{accountForm.value | json}}

Yes is an ugly, ugly hack是的,这是一个丑陋、丑陋的 hack

joke apart, as you has the same number of groups in your form array than in your array "listOfPossibleCompaniesFromAPI"(*) just use listOfPossibleCompaniesFromAPI[i]开玩笑,因为您的表单数组中的组数与数组“listOfPossibleCompaniesFromAPI”(*) 中的组数相同,只需使用 listOfPossibleCompaniesFromAPI[i]

<form [formGroup]="accountForm">
        <div formArrayName="CompanyArray">
      <div *ngFor="let student of CompanyArray.controls; let i=index" [formGroupName]="i">
          <input type="checkbox" formControlName="selected">
          {{ listOfPossibleCompaniesFromAPI[i] }}
          <input formControlName="specialCode" placeholder="Enter special code">
        </div>
    </div>
</form>

(*)Logically, you has a forEach of this array to push a formGroup in the array (*)逻辑上,你有一个forEach这个数组来推送数组中的一个formGroup

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM