I'm trying to use the ComponentFactoryResolver to create a reactive form.
All the components I want to add in that form are specifics, and implement the ControlValueAccessor interface.
So, my question is simple : How can I add form controls on component created dynamically with ComponentFactoryResolver, without modifying my components ?
At the moment, my code is as follows :
component: ComponentRef<any>;
form: FormGroup;
@ViewChild('container', {read: ViewContainerRef}) container: ViewContainerRef;
constructor(private resolver: ComponentFactoryResolver, private fb: FormBuilder) {}
ngOnInit(): void {
this.form = this.fb.group({});
const component: any = MyStringComponent;
const factory: any = this.resolver.resolveComponentFactory<any>(component);
this.component = this.container.createComponent(factory);
}
And the template :
<form
[formGroup]="form">
<ng-container #container>
</ng-container>
</form>
This code works fine, my component is injected where I want, and I can access its Inputs using this.component.instance.
So, where should I add my formControlName directive ?