2个for循环完成后如何return取值?
How to return value when 2 for loops are completed?
我的代码在数据库中进行 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 次。
如何确保在显示值之前完成所有数据库查询和前端计算?
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]);
}
})
}
我的代码在数据库中进行 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 次。
如何确保在显示值之前完成所有数据库查询和前端计算?
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]);
}
})
}