5

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!

2
  • I have the same question. Not that this helps right now, there is 'component outlet' coming in Angular 4, hopefully this will allow this. Commented Feb 9, 2017 at 18:26
  • 1
    Yes is possible, look at this question: stackoverflow.com/questions/44284026/… Commented Jul 25, 2021 at 13:57

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.