简体   繁体   English

如何在构造函数中加载数据,并通过ngAfterViewInit中的Chartjs呈现数据?

[英]How to load data in constructor, and render it via Chartjs in ngAfterViewInit?

Here's my problem. 这是我的问题。 A lot of examples of using Chartjs in Angular only assume that data is already there , and they render it in ngAfterViewInit . 在Angular中使用Chartjs的许多示例仅假设数据已经存在 ,并在ngAfterViewInit渲染。

However, I can't figure out how to meet my requirements: 但是,我不知道如何满足我的要求:

  • I need to load data in the constructor 我需要在构造函数中加载数据
  • While loading data, I set a Boolean attribute for showing an indeterminate mat-spinner 加载数据时,我设置了一个Boolean属性,用于显示不确定的mat-spinner
  • Of course, loading data is truly indeterminate, and I can't know when it's loaded (promise land maybe) 当然,加载数据确实是不确定的,而且我不知道何时加载(可能是承诺土地)
  • When data is loaded, I need to use Chartjs API to draw data into canvas 加载数据后,我需要使用Chartjs API将数据绘制到画布中
  • If I do it in the constructor, then I encounter null reference for document.getElementById('canvasId') , since it's not rendered yet. 如果我在构造函数中执行此操作,那么我会遇到document.getElementById('canvasId')空引用,因为它尚未呈现。
  • If I load data in constrcutor and want to render canvas in ngAfterViewInit , then I don't know when data is loaded, since it's indeterminate, and I encounter null reference for the properties of my data object. 如果我在ngAfterViewInit加载数据并想在ngAfterViewInit渲染画布,那么我不知道何时加载数据,因为它是不确定的,并且我遇到的数据对象属性为null。
  • If I load data in the ngAfterViewInit , then Angular complains that ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'ngIf: undefined'. Current value: 'ngIf: true' 如果我在ngAfterViewInit加载数据,则Angular会抱怨ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'ngIf: undefined'. Current value: 'ngIf: true' ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'ngIf: undefined'. Current value: 'ngIf: true' ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'ngIf: undefined'. Current value: 'ngIf: true' . ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'ngIf: undefined'. Current value: 'ngIf: true' This error is related to mat-spinner *ngIf='progress' 此错误与mat-spinner *ngIf='progress'

I'm stuck at this point on how to reconcile these requirements. 我现在停留在如何调和这些要求上。 Any help? 有什么帮助吗?

This post uses a service to get data and render the chart after the data is loaded. 这篇文章使用服务来获取数据并在加载数据后呈现图表。 Hope it helps! 希望能帮助到你!

Angular Load Data from Service 来自服务的角负载数据

I was getting the expression changed error but loading and executing in OnInit worked for me. 我遇到表达式更改错误,但是在OnInit中加载和执行对我来说很有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM