0

嗨,我是 Angular 新手,目前我收到如下错误。如果您能对此有所帮助,我们将不胜感激。

GlobalSettingsComponent.html:66 ERROR Error: Cannot find control with path: 'tagonSettings -> tagonSettingFormGroup -> tagonType'
    at _throwError (forms.js:2432)
    at setUpControl (forms.js:2300)
    at FormGroupDirective.addControl (forms.js:6664)
    at FormControlName._setUpControl (forms.js:7314)
    at FormControlName.ngOnChanges (forms.js:7227)
    at checkAndUpdateDirectiveInline (core.js:12348)
    at checkAndUpdateNodeInline (core.js:13876)
    at checkAndUpdateNode (core.js:13819)
    at debugCheckAndUpdateNode (core.js:14712)
    at debugCheckDirectivesFn (core.js:14653)

所以以下是我想要做的。我有一个表单组,其中有表单数组,如下所示

this.formGroup = this.fb.group({
      inventories: this.fb.array([]),
      flightRates: this.fb.array([]),
      settings: this.fb.array([]),
      tagonSettings: this.fb.array([])
    });

在 tagonSettings 表单数组中,我正在尝试创建并推送 FormGroup 的实例,如下所示。我推动表单组的原因是因为我需要对一些控件进行分组。

this.globalTagonSettingsArray.push(
  new FormGroup({
      id : new FormControl( globalTagonSetting.id ),
      tagonText: new FormControl(globalTagonSetting.tagonText, [Validators.required] ),
      tagonType: new FormControl(globalTagonSetting.tagonType, [Validators.required] )
    }
  )
);

在我的组件 html 中,我正在执行以下操作。

 <div class="form-title mb-3 mt-3">{{ 'HEADINGS.ADDITIONAL_TAGON_SETTINGS' | translate }}</div>
    <div formArrayName="tagonSettings">
        <div class="row" *ngFor="let tagonSettingFormGroup of globalTagonSettingsArray.controls; let i = index">
          <div formGroupName="tagonSettingFormGroup">
            <div class="col-4">
                <input formControlName="tagonText">
            </div>
            <div class="col-4">
                <input formControlName="tagonType">
            </div>
            <div class="col-4"></div>
          </div>
        </div>
    </div>
4

1 回答 1

0

我应该添加 formGroup]="tagonSettingFormGroup"。现在它开始起作用了。

<div class="form-title mb-3 mt-3">{{ 'HEADINGS.ADDITIONAL_TAGON_SETTINGS' | translate }}</div>
    <div formArrayName="tagonSettings">
        <div class="row" *ngFor="let tagonSettingFormGroup of globalTagonSettingsArray.controls; let i = index">
          <div [formGroup]="tagonSettingFormGroup" >
            <div class="col-4">
                <input formControlName="tagonText">
            </div>
            <div class="col-4">
                <input formControlName="tagonType">
            </div>
            <div class="col-4"></div>
          </div>

        </div>
    </div>
于 2019-11-06T14:45:42.417 回答