组 angular 6 中的嵌套 formarray

Kru*_*ger 5 angular angular-reactive-forms formarray

再会。

我寻找了解决方案,即使有一个与我所问的类似的问题,我相信这个问题更独特一些,而不是重复。

这是我的 html 表单:

<div class="form-group col-md-6" formGroupName="schema">
  <div formArrayName="currencies">
    <input type="text" class="form-control" id="percentage" formControlName="percentage" placeholder="Discount %*" required>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

这是我的 ts formBuilder。

this.createPromo = this.fb.group({
      type: ['promotion'],
      name: ['', Validators.required],
      description: ['', Validators.required],
      enabled: ['', Validators.required],
      promotion_type: ['', Validators.required],
      start: ['', Validators.required],
      end: ['', Validators.required],
      schema: this.fb.group({
        currencies: this.fb.array([
          this.fb.group({
            percentage: '',
            currency: 'ZAR'
          })
        ])
      }),
    });
Run Code Online (Sandbox Code Playgroud)

所以我希望我的表单作为分组数组提交。然而,控制台中的错误如下Cannot find control with path: 'schema -> currencies -> percentage',因此percentage即使我输入了一个数字,我也无法将我的表单提交为空。

Sid*_*era 10

您的场景需要以下内容:

  1. 父母divformGroupName="schema"
  2. 在里面,一个divwith formArrayName="currencies"
  3. 在里面,一个divwith ngFor="let currencyGroup of currencyFormGroups; let i = index;"。请注意,这currencyFormGroups是您的组件类中的一个 getter。
  4. 在其中,一个divwith [formGroupName]="i"wherei是我们在*ngFor.
  5. 实际上,两个inputs分别带有formControlName="percentage"formControlName="currency"

.

以下是所有这些步骤转换为代码:

import { Component } from '@angular/core';
import { FormGroup, FormControl, FormArray, Validators, FormBuilder } from '@angular/forms';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  createPromo: FormGroup;

  constructor(private fb: FormBuilder) { }

  ngOnInit() {
    this.createPromo = this.fb.group({
      'type': ['type'],
      name: ['name', Validators.required],
      description: ['description', Validators.required],
      enabled: ['enabled', Validators.required],
      promotion_type: ['promotion_type', Validators.required],
      start: ['start', Validators.required],
      end: ['end', Validators.required],
      schema: this.fb.group({
        currencies: this.fb.array([
          this.fb.group({
            percentage: 'percentage',
            currency: 'ZAR'
          }),
          this.fb.group({
            percentage: 'percentage',
            currency: 'INR'
          }),
        ])
      }),
    });
  }

  get currencyFormGroups() {
    return (<FormArray>(<FormGroup>this.createPromo.get('schema')).get('currencies')).controls;
  }

}
Run Code Online (Sandbox Code Playgroud)

模板:

<form [formGroup]="createPromo">

  ...

  <div formGroupName="schema">
    <div formArrayName="currencies">
      <div *ngFor="let currencyGroup of currencyFormGroups; let i = index;">
        <div [formGroupName]="i">
          <input 
            type="text" 
            name="percentage"
            formControlName="percentage">
          <input 
            type="text" 
            name="currency"
            formControlName="currency">
        </div>
      </div>
    </div>
  </div>

</form>
Run Code Online (Sandbox Code Playgroud)

这是供您参考的示例 StackBlitz

PS:为简单起见,我已将所有表单控件视为input. 请相应地进行更改。