【发布时间】:2019-09-11 20:02:00
【问题描述】:
我在打字稿代码中得到了 canvas 元素的空值。有一个静态饼图,点击它后,向下钻取到它的子饼图。
在 onclick 功能中,我使用 *ngIf 隐藏父级并显示子级。子图表未显示。
HTML代码:
<div>
<canvas *ngFor="let chart of charts; let i = index" id="canvas{{i}}" #mycanvas></canvas>
</div>
输入脚本代码:
@ViewChildren("mycanvas") divView: ElementRef;
array = [];
ngAfterViewInit(): void {
this.createCharts(this.array);
}
charts = [];
constructor(private elementRef: ElementRef) { }
ngOnInit() {
this.createChartsData();
}
createChartsData() {
for (var i = 0; i < 3; i++) {
var pie = {
type: 'pie',
data: {
labels: ["Disks", "Mgmt", "Hardware", "FC", "Vols&Pols"],
datasets: [{
backgroundColor: ["#008000", "#008000", "#008000", "#008000", "#008000"],
data: [20, 20, 20, 20, 20]
}]
},
options: {
title: { display: false },
animations: true,
tooltips: { enabled: true },
legend: { display: true }
}
};
this.array.push(pie);
}
}
createCharts(pieData) {
console.log(this.elementRef);
console.log(this.divView+" 777777777dfd");
var ctx: CanvasRenderingContext2D = this.divView.nativeElement.getContext("2d");
for (var j = 0; j < 3; j++) {
/* let htmlRef = this.divView.nativeElement.select(`canvas` + j);
console.log(htmlRef);
var tempChart = new Chart(htmlRef, pieData[j]);
this.charts.push(tempChart); */
}
}
this.divView,以 null 的形式出现。你能告诉我上面的代码有什么问题吗?
【问题讨论】: