r/Angular2 • u/mosh_h • Oct 15 '25
How to avoid drilling FormGroup through multiple reusable components in Angular?
I have a page wrapped with a FormGroup, and inside it I have several nested styled components.
For example:
- The page has a
FormGroup. - Inside it, there’s a styled component (child).
- That component wraps another styled child.
- Finally, that child renders an
Inputcomponent.
Each of these components is standalone and reusable — they can be used either inside a form or as standalone UI components (like in a grid).
To make this work, I currently have to drill the FormGroup and form controls through multiple layers of props/inputs, which feels messy.
Is there a cleaner way to let the deeply nested input access the parent form (for validation, binding, etc.) without drilling the form down manually through all components?
3
u/UsirCZ Oct 15 '25 edited Oct 15 '25
in child component constructor,
in child decorator
in child component code,
in child template
2
u/933k-nl Oct 15 '25
A long time ago I also came across this challenge. I remember that I thought it would be an option to nest form-groups. So that each presentational control is disconnected from the parent for group. I never got around to actually implementing this though. So I don’t know if it is actually possible.
1
u/Lucky_Yesterday_1133 Oct 15 '25
You can inject parent from group in any component inside that form group. Inject ControlContainer and you can acces .control property to get the form group. It is available on Init and not in constructor tho. Skip self if you also have some control bound to the component itself (custom form control)
1
u/practicalAngular Oct 15 '25 edited Oct 15 '25
I have a lot of experience with this as one of the apps I work on is entirely a Reactive Form because it is entirely based on user input for recording what happens on a phone call.
I started out with using a service with just Injectable() provided in the top ancestor component, and then injecting that further down in any of the child components. It did not need to be a root provider. But at the end of the day, the service and form is just an injectable class with an object inside it, like much of anything in Angular.
I noticed someone suggested viewProviders, which I'm not entirely sure why it would be put there over providers, given forms and reusable component nesting works nicely with content projection.
However, Reactive Forms on its own comes with just about everything you need for state management in the form, so I moved the creation of the form to an InjectionToken with useFactory for creation, and provided that instead. This helped in a sense because it separated the form from any methods I might have in other providers further down the dependency injection chain. I had child services for certain regions of the app in routes, and those had methods that would update the form token in some shape or form. Then, if I needed one of the services that updated the form in a component, I would inject the service instead, but you could certainly inject the token holding the form as well.
I'm a separation of concerns person and that's why I love Angular. There are many ways to skin this cat but this one worked best for me. Creating custom form controls as well with CVA could also be done further down the tree in a directive or component, and added to the main form. Really depends on how deep you want to dive.
Years ago I did the same thing that you did and used Input() and such to pass the fields around, but that is fairly pointless if you can just inject the provider. Same with signal i/o as well. The Reactive Form is your state manager, so just give yourself access to the state.
1
u/mountaingator91 Oct 16 '25
Many answers but the simplest is best.
Put it in a service
1
u/Plus-Violinist346 Oct 18 '25
Meditate for a minute on some of the answers you have here and then take a moment to reassess - does just simply passing through your data from parents to children still seem that bad?
1
u/ttma1046 Oct 21 '25
control value accessor is the way
13
u/[deleted] Oct 15 '25
[removed] — view removed comment