4

We are currently using the ViewContainerRef.createComponent() method to generate components dynamically at runtime - it's sort of a plugin system, where the user can add plugins and display screens as they wish. The relevant code is here:

 this.module = moduleFactory.ngModuleFactory.create(this.injector);
 var factory = this.module.componentFactoryResolver.resolveComponentFactory(tab.type);
 tab.component = this.container.createComponent(factory);

Where moduleFactory is a factory to create a dynamically loaded module. If I have a directive SomeDirective I would like to apply to the created component, is this possible? The only current way I've seen is through template compilation.

Has anyone had any luck with this? I see the parameter projectableNodes: any[][] in the call to createComponent, but I have no idea if this would be relevant in this case. Thanks!

titusfx
  • 1,896
  • 27
  • 36
  • I have the same question. Not that this helps right now, there is 'component outlet' coming in Angular 4, hopefully this will allow this. – Aardvark Feb 09 '17 at 18:26
  • 1
    Yes is possible, look at this question: https://stackoverflow.com/questions/44284026/creating-a-angular2-component-with-ng-content-dynamically/44289996#comment121012790_44289996 – titusfx Jul 25 '21 at 13:57

0 Answers0