ng2 - dynamically creating a component based on a template

前端 未结 2 1525
天命终不由人
天命终不由人 2020-11-27 05:15

I\'ve been looking at the Angular 2 APIs for ComponentResolver and DynamicComponentResolver for creating dynamic components, but I have something d

2条回答
  •  抹茶落季
    2020-11-27 05:59

    Update2:

    As @estus mentioned in comments version with className won't work with minification. To do it working with minification you can

    1) add some static key on each of your entryComponents like:

    export LineChartComponent {
      static key = "LineChartComponent";
    }
    

    and then use this key as unique.

    const factoryClass = >factories.find((x: any) => x.key === compClassKey);
    

    2) create a dictionary like

    export const entryComponentsMap = {
      'comp1': Component1,
      'comp2': Component2,
      'comp3': Component3
    };
    

    and then

    const factory = this.resolver.resolveComponentFactory(entryComponentsMap.comp1);
    

    Update1:

    Here's version from component`s class name

    const factories = Array.from(this.resolver['_factories'].keys());
    const factoryClass = >factories.find((x: any) => x.name === compClassName);
    const factory = this.resolver.resolveComponentFactory(factoryClass);
    
    • How to load component dynamically using component name in angular2?

    Old version

    You can get factory by component selector but you have to use private property.

    It might look something like:

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

    Plunker Example

提交回复
热议问题