How to use ResolveComponentFactory() but with a string as a key How to use ResolveComponentFactory() but with a string as a key angular angular

How to use ResolveComponentFactory() but with a string as a key


It's either defining a map of available components,

const compMap = {  text: PictureBoxWidget,  image: TextBoxWidget};

Or defining identifiers as static class property that will be used to generate a map,

const compMap = [PictureBoxWidget, TextBoxWidget].map(widget => [widget.id, widget]).reduce((widgets, [id, widget]) => Object.assign(widgets, { [id]: widget }), {});

The map is then used like

let compFactory: ComponentFactory;if (componentName in compMap) {    compFactory = this.compFactoryResolver.resolveComponentFactory(compMap[componentName]);} else {    throw new Error(`Unknown ${componentName} component`);}

There's no way how component classes can be magically identified as strings, because they aren't resolved to strings in Angular 2 DI (something that was changed since Angular 1, where all DI units were annotated as strings).