[英]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.