遍历包含其他模型值的 formArray
Iterate through a formArray containing inside other model values
我有疑问如何遍历 formArray 并使用来自其他数组的内部数据。我在下面添加了示例:
@Component({
selector: 'app-reactive-form-test',
styleUrls: ['./reactive-form-test.component.scss'],
template: `
<form [formGroup]="questionForm">
<ng-container formArrayName="questions">
<ng-container *ngFor="let question of questionControls.controls; let i = index">
<input type="text" [formGroupName]="i">
{{?????? how to display prop1}}
{{?????? how to display prop2}}
</ng-container>
</ng-container>
</form>
`
})
export class ReactiveFormTestComponent implements OnInit {
questionForm: FormGroup;
questions: ModelClass[];
get questionControls() {
return this.questionForm.get('questions') as FormArray;
}
constructor(private fb: FormBuilder) { }
ngOnInit(): void {
this.questionForm = this.fb.group({
questions: this.fb.array([])
});
this.questions = [];
this.questions.push(new ModelClass(1, 1, 1, 1));
this.questions.push(new ModelClass(2, 3, 1, 2));
this.questions.push(new ModelClass(3, 4, 1, 3));
this.questions.forEach(value => {
const control = this.questionForm.get('questions') as FormArray;
control.push(this.fb.group({
id: [value.id],
prop1: [value.prop1]
}));
});
}
}
export class ModelClass {
id,
prop1,
prop2,
prop3
}
我的问题是如何在 formArray 循环数据 rom 模型中显示(问题)- prop2、prop3 ??
<ng-container *ngFor="let question of questionControls.controls; let i= index">
{{questions[i].prop1}}
{{questions[i].prop2}}
{{questions[i].prop3}}
</ng-container>
我有疑问如何遍历 formArray 并使用来自其他数组的内部数据。我在下面添加了示例:
@Component({
selector: 'app-reactive-form-test',
styleUrls: ['./reactive-form-test.component.scss'],
template: `
<form [formGroup]="questionForm">
<ng-container formArrayName="questions">
<ng-container *ngFor="let question of questionControls.controls; let i = index">
<input type="text" [formGroupName]="i">
{{?????? how to display prop1}}
{{?????? how to display prop2}}
</ng-container>
</ng-container>
</form>
`
})
export class ReactiveFormTestComponent implements OnInit {
questionForm: FormGroup;
questions: ModelClass[];
get questionControls() {
return this.questionForm.get('questions') as FormArray;
}
constructor(private fb: FormBuilder) { }
ngOnInit(): void {
this.questionForm = this.fb.group({
questions: this.fb.array([])
});
this.questions = [];
this.questions.push(new ModelClass(1, 1, 1, 1));
this.questions.push(new ModelClass(2, 3, 1, 2));
this.questions.push(new ModelClass(3, 4, 1, 3));
this.questions.forEach(value => {
const control = this.questionForm.get('questions') as FormArray;
control.push(this.fb.group({
id: [value.id],
prop1: [value.prop1]
}));
});
}
}
export class ModelClass {
id,
prop1,
prop2,
prop3
}
我的问题是如何在 formArray 循环数据 rom 模型中显示(问题)- prop2、prop3 ??
<ng-container *ngFor="let question of questionControls.controls; let i= index">
{{questions[i].prop1}}
{{questions[i].prop2}}
{{questions[i].prop3}}
</ng-container>