angular - 来自 component.ts 的数据不会在 Angular 应用程序的 component.html 中获取
问题描述
我编写了一个简单的 Angular 应用程序,它将从 REST Api 获取数据并绘制饼图。在我的 app.component.ts 文件中,我得到这样的数据
this.satisfactionService.getGenderDetails().subscribe(result =>{
this.Data=result;
console.log(this.Data)
console.log(Object.keys(this.Data).length)
console.log(this.Data.labels)
console.log(this.Data.values)
this.flag=true;
console.log(this.Data)
if (typeof(this.Data) != "undefined") {
this.pieChartLabels = this.Data.labels;
this.pieChartData=this.Data.values;
this.pieChartType= 'pie';
console.log(this.pieChartLabels )
console.log(this.pieChartData)
this.flag=false;
}
}, error => console.log(error));
在我的 app.component.html 文件中,我正在根据数据绘制饼图
<div class="otherBox" >
<div class="vamBox">
<div style="text-align:center;">
<span>
<p class="cardText">Distribution of Students Based on Gender</p>
</span>
</div>
</div>
<div class="vamInfoBox">
<canvas baseChart height="100px"
[data]="pieChartData"
[labels]="pieChartLabels"
[chartType]="pieChartType">
</canvas>
</div>
</div>
解决方案
在组件尝试渲染图表后肯定会获取数据,您可以添加一个简单的 ****ngIf*** 指令来检查pieChartDate是否可用
<canvas *ngIf='pieChartData'></canvas>
推荐阅读
- spring - 收到致命警报:protocol_version 和“parent.relativePath”指向错误的本地 POM
- java - 根据从另一个表中选择的输出更新另一个表中的行,但程序只更新最上面的一行
- c++ - 将 exprtk 与自定义类的对象一起使用
- angular - 未进行 API 调用,因为它未反映在 Google chrome 开发人员工具的网络选项卡中
- c++ - OpenCV 4.1.2 - 从网络摄像头获取帧并将其拆分
- r - 如何根据R中数据集的列名在多个图中添加数学表达式标题?
- vue.js - vue js:如何将焦点设置在禁用的输入字段上
- ios - 只想添加一个变暗层或通过 Swift 使 UIImageView 变暗
- javascript - 在数组中收集具有相同 id 的数组并创建具有 id 的新数组,并命名为 javascript
- mysql - 生成(虚拟/存储)列的空间索引?