[英]Setting a class scope variable before calling a function
I have a component that creates a d3 chart. 我有一个创建d3图表的组件。 It creates the chart in a
div
element based on its id
, so I'm setting the id
of the div
during the initiation of the component and then calling a private function that creates the chart: 它基于其
id
在div
元素中创建图表,因此我在组件初始化期间设置div
的id
,然后调用创建图表的私有函数:
HTML: HTML:
<div class="widget">
<div class="header">Progress Status</div>
<div id="{{chartId}}" class="gauge-chart-container"></div>
</div>
JavaScript: JavaScript的:
public ngOnInit(){
this.chartId = 'chart-'+uuid();
this.createGaugeChart(this.chartId);
}
The problem here is the function createGaugeChart
runs before the div elements gets the id
assigned, so when I add a timeout
function it works: 这里的问题是函数
createGaugeChart
在div元素获得分配的id
之前运行,因此当我添加timeout
函数时,它将起作用:
public ngOnInit(){
this.chartId = 'chart-'+uuid();
setTimeout(() => {
this.createGaugeChart(this.chartId);
},1000);
}
I'd rather not add a timeout
of course, so is there a way I can wait on the HTML to render with the scope variables before calling any functions? 我当然不希望添加
timeout
,所以有什么方法可以在调用任何函数之前等待HTML使用范围变量呈现吗? Or maybe I'm calling the createGaugeChart
function in the wrong location? 还是我在错误的位置调用了
createGaugeChart
函数?
Have a look here: https://angular.io/guide/lifecycle-hooks . 在这里看看: https : //angular.io/guide/lifecycle-hooks 。
I think you are probably after ngAfterViewInit() 我认为您可能在ngAfterViewInit()之后
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.