所以,经过一番思考,我解决了这个问题!
所以,如果你按照教程,你就会有类似下面的代码在你的组件:
export class HeroDetailComponent3 {
heroForm: FormGroup; // <--- heroForm is of type FormGroup
constructor(private fb: FormBuilder) { // <--- inject FormBuilder
this.createForm();
}
createForm() {
this.heroForm = this.fb.group({
name: '', // <--- the FormControl called "name"
});
}
}
所以我做了什么,我改变了FormGroup
是@Input() heroForm: FormGroup = null
,
我也加
innerMainAt(index: number): FormGroup {
return this.inners.at(index) as FormGroup;
}
,我已经通过以下方式使用它在html
:用于获取FormGroup
索引的方法
<div formArrayName="inners">
<div *ngFor="let inner of inners.controls; let i=index;" [formGroupName]="i">
// VVV this is the component, and it creates itself
<app-hero-form [heroForm]="innerMainAt(i)"</app-hero-form> // notice innerMainAt(i)
</div>
</div>