angular - ng2 - dynamically creating a component based on a template -


i've been looking @ angular 2 apis componentresolver , dynamiccomponentresolver creating dynamic components, have different in mind apis offer.

is there way in ng2 create component based on string of class name?

for example, im building configurable charts dashboard. each user's layout stored in database, stating want 2x line charts here, 3x bar charts there, etc.

when load data json looks like:

user.charts = [      { type: 'linechartcomponent', position: ... }      { type: 'barchartcomponent', position: ... } ]; 

where type component's class name want reflectively create.

so far have following:

 this.chartmap = {     'linechartcomponent': linechartcomponent  };  let config = this.configuration; let chartcomponenttype = this.chartmap[config.type]; let factory = this.componentfactory.resolvecomponentfactory(chartcomponenttype); let component = factory.create(this.injector); component.instance.configuration = config; this.chartcontainer.insert(component.hostview); 

but whole idea eliminate need chartmap. how can reflectively create class based on string without having reference type?

update2:

as @estus mentioned in comments version classname won't work minification. working minification can

1) add static key on each of entrycomponents like:

export linechartcomponent {   static key = "linechartcomponent"; } 

and use key unique.

const factoryclass = <type<any>>factories.find((x: any) => x.key === compclasskey); 

2) create dictionary like

export const entrycomponentsmap = {   'comp1': component1,   'comp2': component2,   'comp3': component3 }; 

and then

const factory = this.resolver.resolvecomponentfactory(entrycomponentsmap.comp1); 

update1:

here's version component`s class name

const factories = array.from(this.resolver['_factories'].keys()); const factoryclass = <type<any>>factories.find((x: any) => x.name === compclassname); const factory = this.resolver.resolvecomponentfactory(factoryclass); 

old version

you can factory component selector have use private property.

it might like:

const factories = array.from(this.resolver['_factories'].values()); const factory = factories.find((x: any) => x.selector === selector); 

plunker example


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -