javascript - 添加数据后需要刷新Angular组件
问题描述
对我来说很难解决这个问题。我有一个组件从 2 个不同的 url 获取 Json 数据来创建图表:
- 1 更新数据用于旧值
- 另一个为实时值
这是组件:
@Component({
selector: 'app-staticchart',
templateUrl: './staticchart.component.html',
styleUrls: ['./staticchart.component.scss']
})
export class StaticchartComponent implements OnInit {
candles: Observable<Candlecollect[]>;
data: Array<any>;
candlecollect: Candlecollect;
start = Date.now();
end = Date.now() + 1;
constructor(private candlecollectService: CandlecollectService,
public spinnerService: SpinnerService) {
}
ngOnInit(): void {
this.loadChart();
}
loadChart(): void {
/*
* Request to get the 500 latest Candlesticks - send by rest to the back api
*/
const answer = this.candlecollectService.extractCandles('url_1')
.subscribe(data => {
// tslint:disable-next-line:forin
for (let counter = 1; counter < 500; counter++) {
candleSeries.update({
// @ts-ignore
time: data.openTime / 1000,
open: data.open,
high: data.high,
low: data.low,
close: data.close
});
}
});
/*
* Request send by REST to get the real time values
*/
const binanceSocket = new WebSocket('url_2');
binanceSocket.onmessage = function(event) {
const message = JSON.parse(event.data);
// console.log(event.data);
const candlestick = message.k;
candleSeries.update({
// @ts-ignore8
time: candlestick.t / 1000,
open: candlestick.o,
high: candlestick.h,
low: candlestick.l,
close: candlestick.c
});
};
}
}
这是html:
<mat-card class="dashboard-card">
<div id="chart" fxFill></div>
</mat-card>
它工作得很好,但我有这个问题:我需要刷新页面或图表没有出现。
所以我试图让 SpinnerService 等待加载,但它不起作用。
如果这个问题看起来很愚蠢,我很抱歉,但我使用 angular 工作了 1 个月,而且有点复杂......^^
解决方案
推荐阅读
- algorithm - 生成数字的产品分区
- r - 如何基于过滤R中的所有其他变量返回一列的所有唯一值
- laravel-5 - Laravel 5.6 API 资源未显示关系数据
- symfony - 使用 Doctrine 验证模式的数据库错误
- postgresql - PostgreSQL 和 Tableau
- r - 如何从 R 中的线性回归预测单个值?
- r - 如何在R中计算前几年的总和?
- java - 结合 JSON 对象和 JSON 数组以在 Java 中获取另一个 JSON 对象
- bash - 如何将ICP集群登录放入shell脚本
- node.js - 如果我使用无服务器架构,是否会执行 JIT 优化?