i have some form components which in fact just wrap some parts of a big formular into little pieces. I can imagine two ways to do that: a ControlValueAccessor or s simple PropertyBinding to pass in the FormControl to the sub-component.
Super-Simple-Example https://stackblitz.com/edit/angular-dt6pva
Core ideas:
...
providers: [
{
provide: NG_VALUE_ACCESSOR,
multi: true,
useExisting: forwardRef(() => Input01Component)
}
]
})
export class Input01Component implements ControlValueAccessor {
...
vs.
...
@Input()
control = new FormControl();
...
The ControlValueAccessor-way feels a bit more "right", but is more "work" as well.
The question: are there any aspects / advantages / disadvantages when using the simpler FormControl-Input-Binding?
EDIT
Think about this json structure which should be designed as form
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
}
I would define 3 components: PersonForm, AdressForm, CompanyForm
advantages/disadvantages which comes to my head, somebody may add some points i didn't see:
FormControl-Input
- ++simpler sub-components
- --different properties for the usage ( i can't re-use formControl /formControlName)
ControlValueAccessor
- ++same usage as all other input-components (formControlName)
- --"more work" to implement
- ??split up a large form definitions. On one hand building small components is a good point in general and i may can reuse the form-component-parts. On the other hand i "loose" the overall view of the data structure which may be send directly to a rest backend:
peronForm = new FormGroup({
id: new FormControl(),
name: new FormControl(),
username: new FormControl(),
email: new FormControl(),
address: new FormControl(), //>> handled by AdressComponent
company: new FormControl() //>> handled by CompanyComponent
});
Until now: unsure if this is good or bad :)
Thanks for helping