我正在使用 FormArray,然后在其中包含 Form Group,但无法通过 *ngFor 在 html 中访问。我尝试了很多东西,但没有奏效。这是打字稿代码。
private readonly TRAINING_FORM: any = new FormGroup({
// new FormControl('', Validators.required)
codeSchool: new FormControl('', Validators.required),
adressStreetInst: new FormControl('', Validators.required),
adressCityInst: new FormControl('', Validators.required),
adressStateInst: new FormControl('', Validators.required),
adressZipInst: new FormControl('', Validators.required),
phoneInst: new FormControl('', Validators.required),
isProgramInst: new FormControl('', Validators.required),
faxInst: new FormControl('', Validators.required),
noteInst: new FormControl('', Validators.required),
// trainingExtraInformation: new FormArray([])
});
ngOnInit() {
this.loadForm();
}
loadForm() {
const trainingArray = new FormArray([this.TRAINING_FORM]);
const totalInst = new FormControl('', Validators.required);
this.trainingForm = new FormGroup({
totalInst,
trainingArray
});
}
这里是 html ngFor。
<div formArrayName="trainingArray">
<div *ngFor="let training of trainings; index as idx" formArrayName="idx">
<div class="form-group row">
<label class="col-sm-4 col-form-label">Training institution/hospital name
<span class="text-danger">*</span>
</label>
<div class="col-sm-8">
<input type="text" class="form-control" placeholder="No abbreviations, please"
[formControlName]="idx">
</div>
</div>
</div>