我的代码在数据库中进行for循环查询,并在前端进行计算。
仅在计算完成且所有for循环完成后,如何显示值?
这是我的过程:
startQuery() {
this.loading = true;
const queryInfo = {
//from form field
}
this.report = {};
//first DB query
this.angularService.getTime(queryInfo).subscribe(data => {
const first = data.accounts;
for (let i = 0; i < first.length; i++) {
this.report[first[i].firstName] = {
firstName: first[i].firstName,
};
const totalDays = 30;
for (let dayNum = 0; dayNum <= totalDays; dayNum++) {
const employeeId = first[i].eeid;
//Second query from DB
this.angularService.getTimeRecord(employeeId, ).subscribe(data => {
//process data
})
}
}
this.resultDetails = []
for (var subObj in this.report) {
this.resultDetails.push(this.report[subObj]);
}
})
this.loading = false;
}
在我的html中,如果this.loading = true,则应该显示微调框,并且仅当this.loading为false时才显示数值。结果是 微调器会暂时显示并显示值,但是显示通常不完整。在显示的值完成之前,我必须单击startQuery按钮2或3次。
如何在显示值之前确保所有数据库查询和前端计算已完成?
答案 0 :(得分:1)
startQuery() {
this.loading = true;
const queryInfo = {
//from form field
}
this.report = {};
//first DB query
this.angularService.getTime(queryInfo).subscribe(data => {
const first = data.accounts;
for (let i = 0; i < first.length; i++) {
this.report[first[i].firstName] = {
firstName: first[i].firstName,
};
const totalDays = 30;
let responseCount = 0;
for (let dayNum = 0; dayNum <= totalDays; dayNum++) {
const employeeId = first[i].eeid;
//Second query from DB
this.angularService.getTimeRecord(employeeId, ).subscribe(data => {
responseCount ++;
if (responseCount == totalDays){
this.loading = false;
}
//process data
})
}
}
this.resultDetails = []
for (var subObj in this.report) {
this.resultDetails.push(this.report[subObj]);
}
})
}