簡體   English   中英

在Angular 4中動態加載圖表組件嗎?

[英]Load chart components dynamically in Angular 4?

我在Angular 4中創建了許多可重用的圖表組件。所有用於繪制圖表的數據均來自REST API。 當我加載父組件時,將以以下格式獲取圖表列表的數據。

[
   {
      "chartId":1,
      "chartType":"Bar"
   },
   {
      "chartId":2,
      "chartType":"Pie"
   }
]

我正在為圖表組件使用屬性選擇器。 例如,

<div app-bar-chart></div>

我想做的是,我需要遍歷圖表列表JSON,然后根據“ chartType”設置屬性。 例如,如果“ chartType”為“ Pie”,則元素應為,

<div app-pie-chart></div>

我該如何實施? 謝謝。

您可以設置屬性,但不會在那里啟動組件。

這是您的操作方法。 創建一個JSON,其中包含對組件類的引用,而不是字符串:

export class BarComponent {...}
export class PieComponent {...}

...

const components = [
    {
        "chartId": 1,
        "chartType": BarComponent < ---component class reference
    },
    {
        "chartId": 2,
        "chartType": PieComponent
    }
]

然后使用NgComponentOutlet渲染這些組件:

<ng-container *ngFor="let c of components">
     <ng-container *ngComponentOutlet="c"></ng-container>

閱讀這些文章以獲取更多信息:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM