I have one custom control component <some-input>
that i wrapped to <ext-some-input>
. SomeInput is encapsulated, has own API and supports reactive forms.
ExtSomeInput is created as high-level wrapper over SomeInput.
I have following html:
<form [formGroup]="form">
<ext-some-input formControlName="name">
</form>
and ExtSomeInput's html:
<some-input formControlName="_???_"></some-input>
The question is how to forward formControlName to inner SomeInput component? I need to tie the form and inner formControl up. Is this possible?
EDITED:
I've created stackblitz project with this issue: here
Hey making a second answer here because we came up at work with a generic approach that we released as an open source library =).
You can find it here: https://github.com/cloudnc/ngx-sub-form
It should help you manage: - nested forms - forms with polymorphic data - better typings
Everything is explained into the readme and a complete example is provided into the /src folder. (lib is actually in projects/ngx-sub-form).
Live demo available here too: https://cloudnc.github.io/ngx-sub-form
I think that you're actually looking something a bit more complex than an
input
.If you want to create a customized input you can create a class which is going to be a
ControlValueAccessor
(custom form control).Please check the following article where it's really well explained:
https://alligator.io/angular/custom-form-control
ext-some-input.ts
ext-some-input.html
I've run into a similar scenario where I did not want to pass formControlName to the wrapper component. My preferred way to solve this is to just reuse the formGroup passed from the parent form in the wrapper component. You can do this by injecting ControlContainer in the constructor of ExtSomeInput component:
ParentForm.component.html
ExtSomeInput.component.ts
ExtSomeInput.component.html
Your inner component can take
@Input
controlName
but it won't work out of the box:In order to tie your control with parent FormGroup you can define
viewProvider
as follows:Forked Stackblitz