I have an Angular 15 application (see source code on Stackblitz) that uses reactive forms and a ControlValueAccessor pattern to create a parent form which includes child form groups. When I add a form group and annotate it as a FormGroup in the parent html template, the data is not passed to the child. When I annotate it as a FormControl it is passing and accessing the data of the child form correctly, but I get the error
ERROR Error: control.registerOnChange is not a function
Furthermore I can't access the individual controls inside the FormGroup if I annotate it as a FormControl.
In the code example childGroupForm2 receives the passed values but childGroupForm does not.
My optimal solution would be to annotate the FormGroup as a FormGroup and pass the values from the parent component to the child.
Does anyone know why this is not working with FormGroups but works with FormControl?
Edit for minimal code:
// parent.component.html
<form [formGroup]="parentForm">
<child-group [formGroup]="childGroupForm"> </child-group>
<child-group [formControl]="childGroupForm2"> </child-group>
</form>
// parent.component.ts
get childGroupForm(): FormGroup {
return this.parentForm.get('childGroupForm') as FormGroup;
}
get childGroupForm2(): FormControl {
return this.parentForm.get('childGroupForm2') as FormControl;
}
ngOnInit() {
this.parentForm = this.fb.group({
childGroupForm: this.fb.group({
elementInput: 'Test1',
elementsSelectionInput: 'Test2',
}),
childGroupForm2: this.fb.group({
elementInput: 'Test3',
elementsSelectionInput: 'Test4',
}),
});
}