簡體   English   中英

Angular2 @ViewChild 沒有從 ng2-charts 中獲取 BaseChartDirective

[英]Angular2 @ViewChild is not picking up BaseChartDirective from ng2-charts

我正在使用 ng2-charts v1.5.0,並且我試圖讓圖表在單擊后使用新數據進行更新。 我正在關注其他人發布的示例。

在我看來,我有:

<div style="display: block">

  <canvas #chart baseChart
              [data]="dChartData"
              [labels]="dChartLabels"
              [chartType]="dChartType"
              (chartClick)="chartClicked($event)">
  </canvas>

</div>

如果我寫:

@ViewChild( BaseChartDirective ) chart: BaseChartDirective;

然后我得到:

__zone_symbol__error : Error: Can't construct a query for the property "chart" of "ChartTest1WidgetComponent" since the query selector wasn't defined.

如果我寫:

@ViewChild( 'chart' ) chart: BaseChartDirective;

然后我調用this.chart.ngOnChanges({}); ,但我得到: _this.chart.ngOnChanges is not a function

這對我來說沒有意義。 如何獲得引用BaseChartDirective的正確視圖子BaseChartDirective

這對我@ViewChild(BaseChartDirective) private Chart: BaseChartDirective;@ViewChild(BaseChartDirective) private Chart: BaseChartDirective; 然后this.Chart.ngOnChanges({} as SimpleChanges)

但是,更簡單的方法是用新數據實際更新變量 dChartData,它應該自動用新數據更新您的圖表。 它比使用 ngOnChanges 更快。 希望這可以幫助!

暫無
暫無

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

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