2017-02-22 1 views

答えて

1

動的コンポーネントの作成に使用できます。これは特に使用されます。私たちは、あなたがそれをページのentryComponentsセクションで定義されたコンポーネントに対応する工場を返すファクトリにあなたのコンポーネント名を渡す場合

を注入したい部品の工場を取得するために使用し

。あなたが別の一連のグラフが含まれているダッシュボードを持っている場合、あなたはライン、バーすなわち、異なるグラフを使用して同じデータを表示したい場合があり例えば

...

あなたが使用DynamicComponent注入しますたとえば、BarChartComponentLineGraphComponentなどの任意の要素を入力します。

このファクトリは、そのコンポーネントを解決するために使用されます。

オブジェクトツーパスin.ts

public standard: any = 
{ 
    Component: LineGraphComponent, 
    Inputs: { 
     GraphData: { 
      //...graph data 
     } 
    } 
}; 

dynamic.component.ts

import { Component, Input, ViewContainerRef, ViewChild, ReflectiveInjector, ComponentFactoryResolver } from "@angular/core"; 
import { BarChartComponent } from "./barchart.component"; 
import { LineGraphComponent } from "./linegraph.component"; 

@Component({ 
    selector: "hfa-dynamic-search", 
    entryComponents: [BarChartComponent , LineGraphComponent], 
    templateUrl: "app/app.component.html", 
    styleUrls: ["app/app.component.css"] 
}) 
export calss DynamicComponent { 

    @ViewChild("dynamicComponent", { read: ViewContainerRef }) protected dynamicComponent: ViewContainerRef; 

    @Input() 
    set componentData(data: any) { 
     ... 
     //Creates a factory out of the passed in component name 
     let factory = this.resolver.resolveComponentFactory(data.Component); 

     //Creates the component out of the factory with the input values we want to inject 
     let component = factory.create(injector); 

     //Inserts it into the view 
     this.dynamicComponent.insert(component.hostView); 
     ... 
    } 

    constructor(private resolver: ComponentFactoryResolver) { } 
} 

dynamic.component.html

<div #dynamicComponent></div> 

Crどのように実装されているかについての偉大なブログの投稿に編集するhere

希望に役立ちます。

+0

それはその目的に答えるものですが、内部的にはその目的には答えません。 –

関連する問題