Created
March 12, 2017 13:35
-
-
Save brakmic/c86d5ad5892e208958c2618a3e3c09f6 to your computer and use it in GitHub Desktop.
helper function for dynamic component creation in Angular 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function createComponentFactory(compiler: Compiler, | |
metadata: Component): | |
Promise<ComponentFactory<any>> { | |
const cmpClass = class DynamicComponent {}; | |
const decoratedCmp = Component(metadata)(cmpClass); | |
@NgModule({ | |
imports: [ CommonModule ], | |
declarations: [decoratedCmp], | |
schemas: [ CUSTOM_ELEMENTS_SCHEMA ] }) | |
class DynamicHtmlModule { } | |
return compiler.compileModuleAndAllComponentsAsync(DynamicHtmlModule) | |
.then((moduleWithComponentFactory: ModuleWithComponentFactories<any>) => | |
{ | |
return moduleWithComponentFactory.componentFactories.find | |
((x) => x.componentType === decoratedCmp); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment