Skip to content

Instantly share code, notes, and snippets.

@brakmic
Created March 12, 2017 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brakmic/c86d5ad5892e208958c2618a3e3c09f6 to your computer and use it in GitHub Desktop.
Save brakmic/c86d5ad5892e208958c2618a3e3c09f6 to your computer and use it in GitHub Desktop.
helper function for dynamic component creation in Angular 2
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