Я понимаю, что у вас есть несколько "учеников", так что вы можете иметь массив FormGroups, а не formArray-
primaryForm:FormGroup[]=[]
На самом деле у вас есть функция createForm, которая возвращает formGroup
createFormGroup():FormGroup{
return this.formBuilder.group({
firstname:['',Validators.required],
lastname:['',Validators.required],
socialcontrol:this.addSocialControls()
})
}
Чтобы запросить formArray, вы добавляете индекс
get socialsArray(index){
return <FormArray>this.primaryForm[index].get('socialcontrol');
}
В ngOnInit
ngOnInit()
{
this.primaryForm.push(this.createFormGroup())
}
И ваш .html
<div *ngFor="let formGroup of primaryForm;let index=index">
<div [formGroup]="formGroup">
<input formGroupName="firstname">
<input formGroupName="lastname">
<!--here your array, see how use "index"-->
<label *ngFor="let social of socialsArray(index).controls;let i=index;"
class="kt-checkbox">
<!--To know what check, pass as argumnet "index" too-->
<input (change)="updateCheckboxList(index,secondaryForm)"
[formControl]="social"
type="checkbox" id="secform{{socials[i].name}}"> {{socials[i].name}}
<span></span>
</label>
</div>
</div>
ПРИМЕЧАНИЕ. не понимаю вашу функцию updateCheckboxList (index, secondForm). В целом, в ReactiveForms предпочтительнее использовать
primaryForm[index].get('social').valueChanges
.subscribe(res=>{
//here you has the array of values
})
Ну, действительно нужно сделать что-то вроде
ngOnInit()
{
this.primaryForm.push(this.createFormGroup())
this.primaryForm[0].get('social').valueChanges
.subscribe(res=>{
//here you has the array of values
})
}
И, когда добавить новую форму
addForm()
{
this.primaryForm.push(this.createFormGroup())
this.primaryForm[this.primaryForm.length-1].get('social').valueChanges
.subscribe(res=>{
//here you has the array of values
})
}