Is it possible to add form control to a (in component.ts
) created component instance?
I dynamically create a component with the componentFactoryResolver
and render it in a ng-template.
Since i cannot add the directive formControlName
to a ng-template i need to add this to the instance of the new created component in the component.ts
.
How can this be done - or even is this possible to do?
the new component will be created by the directive myCustomComponentDirective
.
component.html:
<form [formGroup]="formGroup">
<ng-container *ngFor="let singleForm of formConfigs;">
<ng-container [ngSwitch]="singleForm?.type">
<ng-container *ngSwitchCase="'input'">
<input formControlName="singleForm.conrolName" >
</ng-container>
<ng-container *ngSwitchCase="'custom'">
<!-- here i want to add form support: #customForm -->
<ng-template myCustomComponentDirective #customForm></ng-template>
</ng-container>
</ng-container>
</ng-container>
</form>
component.ts:
@Component({
selector: 'custom',
templateUrl: './component.html',
})
export class Component
{
@ViewChild("customForm") private _customForm: TemplateRef;
constructor(){}
addFormControl(){
//here i want to add the formControl or formControlName to the component which implements controlValueAccessor
}
}