簡體   English   中英

在調用函數之前設置類范圍變量

[英]Setting a class scope variable before calling a function

我有一個創建d3圖表的組件。 它基於其iddiv元素中創建圖表,因此我在組件初始化期間設置divid ,然后調用創建圖表的私有函數:

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.

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