[英]Setting a class scope variable before calling a function
我有一個創建d3圖表的組件。 它基於其id
在div
元素中創建圖表,因此我在組件初始化期間設置div
的id
,然后調用創建圖表的私有函數:
HTML:
<div class="widget">
<div class="header">Progress Status</div>
<div id="{{chartId}}" class="gauge-chart-container"></div>
</div>
JavaScript的:
public ngOnInit(){
this.chartId = 'chart-'+uuid();
this.createGaugeChart(this.chartId);
}
這里的問題是函數createGaugeChart
在div元素獲得分配的id
之前運行,因此當我添加timeout
函數時,它將起作用:
public ngOnInit(){
this.chartId = 'chart-'+uuid();
setTimeout(() => {
this.createGaugeChart(this.chartId);
},1000);
}
我當然不希望添加timeout
,所以有什么方法可以在調用任何函數之前等待HTML使用范圍變量呈現嗎? 還是我在錯誤的位置調用了createGaugeChart
函數?
在這里看看: https : //angular.io/guide/lifecycle-hooks 。
我認為您可能在ngAfterViewInit()之后
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.