繁体   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