收到错误“ TypeError:无法在字符串“ control”上创建属性“ validator””

Tom*_*nes 3 typescript angular

我收到错误消息:TypeError:无法在字符串“ control”上创建属性“ validator””

import { Component, ChangeDetectionStrategy, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { BrowserModule } from '@angular/platform-browser';
import { DataStorageService, FieldConfig, ImportType } from '../../../../services/datastorage.service';
import { ReactiveFormsModule, FormGroup, FormArray, FormControl } from '@angular/forms';


import * as Papa from 'papaparse';
@Component({
  selector: 'field-analysis',
  templateUrl: './field-analysis.component.html'
})

export class FieldAnalysisComponent implements OnInit {

  fieldsGroup;

  ngOnInit() {
    this.fieldsGroup = new FormGroup({ fieldset: new FormArray([new FormControl(''), new FormControl('')]) });

  }

}
Run Code Online (Sandbox Code Playgroud)

模板看起来像这样。

<h2 >Field Definition</h2>
<form formGroup="fieldsGroup" novalidate>


<input *ngFor="let control of fieldsGroup.controls['fieldset'].controls" formControl="control">
</form>
Run Code Online (Sandbox Code Playgroud)

yur*_*zui 5

已将字符串传递fieldsGroupFormGroupDirective。您应该FormGroup改为传递实例:

[formGroup]="fieldsGroup"
Run Code Online (Sandbox Code Playgroud)

而且FormControlDirective也希望FormControl实例

[formControl]="control"
Run Code Online (Sandbox Code Playgroud)