0

I'm creating components dynamically but when I try to do it with a directive I failed. How can I create/instantiate a directive dynamically and assign/associate it with a component?

I create components in the following way:

createComponent(anyViewContainer: ViewContainerRef, aComponentClass: Type<any>) {
    const aComponentFact = this.componentFactoryResolver.resolveComponentFactory(aComponentClass);
    const aComponentRefs = anyViewContainer.createComponent(aComponentFact);

    return aComponentRefs;
  }
Me the scripter
  • 137
  • 1
  • 14
  • As of this awnser it is not possible: https://stackoverflow.com/questions/39563547/how-to-instantiate-and-apply-directives-programmatically – Fussel Jul 25 '21 at 16:00
  • Have you tried adding the Directive in the template of the dynamically injected cmp? Add ElementRef in the Directive constructor and you can access the directive element directly – KQI Jul 25 '21 at 19:56

0 Answers0