I have been working on the implementation of model driven form for an application with many nested components.
It is an snippet of my current template
<div [formGroup]="formGroup">
<mycomponent1 [formParam]="getForm('myForm1')"></mycomponent1>
<mycomponent2 [formParam]="getForm('myForm2')"></mycomponent1>
<mycomponent3 [formParam]="getForm('myForm3')"></mycomponent1>
</div>
.ts file snippet
ngOnInit() {
this.formGroup= this.fb.group({
myForm1 = this.fb.group({...}),
myForm2 = this.fb.group({...}),
myForm3 = this.fb.group({...})
});
}
getForm(formName: string) {
return <FormGroup>this.formGroup.get(formName);
}
My question is: does anyone know some other approach in order to implement a model driven form to nested components without the dependency to define an @Input
form group parameter to the children components?
I mean, how to avoid to do this for every nested component?
@Input() formParam: FormGroup;
Why I'm asking that, because I have more nested components and I'm forced to define a formgroup as @Input
in the rest of nested components which is very heavy work.
Cheers.