我试图构建一个向用户显示多个图表的Range2应用程序,而且我在处理多个并行独立http请求时遇到了实际问题,当我在ngOnInit中调用超过2个请求时,我得到了505个响应。
this._service.getPlant().subscribe(plants => {
for (var i = 0; i < plants.length; i++)
for (var name in plants[i]) {
this.plants.push(plants[i][name]);
console.log(plants[i][name]);
}
this._service.getDept().subscribe(depts => {
for (var i = 0; i < depts.length; i++)
for (var name in depts[i]) {
this.depts.push(depts[i][name]);
console.log(depts[i][name])
}
this._service.getMachines().subscribe(machines => {
for (var i = 0; i < machines.length; i++)
for (var MachineName in machines[i]) {
machines.push(machines[i][MachineName]);
// console.log(machines[i][MachineName]) ;
}
});
});
});发布于 2017-07-01 19:53:58
您正在subscription中键入所有服务调用,而应该让它们独立于下面,
this._service.getPlant().subscribe(plants => {
for (var i=0; i<plants.length; i++)
for (var name in plants[i]) {
this.plants.push(plants[i][name]);
console.log(plants[i][name]) ;
}
});
this._service.getDept().subscribe(depts => {
for (var i=0; i<depts.length; i++)
for (var name in depts[i]) {
this.depts.push(depts[i][name]);
console.log(depts[i][name])
}
});
this._service.getMachines().subscribe(machines => {
for (var i=0; i<machines.length; i++)
for (var MachineName in machines[i]) {
machines.push(machines[i][MachineName]);
// console.log(machines[i][MachineName]) ;
}
});根据评论更新:
在完成上一个后引发另一个请求
ngOnInit(){
this._service.getPlant().subscribe(plants => {
for (var i=0; i<plants.length; i++)
for (var name in plants[i]) {
this.plants.push(plants[i][name]);
console.log(plants[i][name]) ;
}
},(error)=>{},
()=>{
/////////////////////////////
// Completion event handler
/////////////////////////////
this.getDepartments();
});
private getDepartments(){
this._service.getDept().subscribe(depts => {
for (var i=0; i<depts.length; i++)
for (var name in depts[i]) {
this.depts.push(depts[i][name]);
console.log(depts[i][name])
}
},(error)=>{},
()=>{
/////////////////////////////
// Completion event handler
/////////////////////////////
this.getMachines();
});
}
private getMachines(){
this._service.getMachines().subscribe(machines => {
for (var i=0; i<machines.length; i++)
for (var MachineName in machines[i]) {
machines.push(machines[i][MachineName]);
// console.log(machines[i][MachineName]) ;
}
});
}
}https://stackoverflow.com/questions/44864803
复制相似问题