http 请求后休眠 angular 2
Sleep after http request angular 2
我正在尝试构建一个 angular 2 应用程序,我在 NgOnInit 中收到 2 个不能同时 运行 的 http 调用,我想休眠几秒钟或执行第一个请求,然后执行第二个一。
这是我的代码
public ngOnInit(): void {
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]);
}
});
this._service.getDept().subscribe(depts => {
for (var i=0; i<depts.length; i++)
console.log(depts[i])
for (var name in depts[i]) {
this.depts.push(depts[i][name]);
}
});
}
有什么解决办法吗?
您可以在收到 getPlant() 的响应后 运行 getDept()。
public ngOnInit(): void {
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]);
}
this._service.getDept().subscribe(depts => {
for (var i = 0; i < depts.length; i++)
console.log(depts[i])
for (var name in depts[i]) {
this.depts.push(depts[i][name]);
}
});
});
}
我正在尝试构建一个 angular 2 应用程序,我在 NgOnInit 中收到 2 个不能同时 运行 的 http 调用,我想休眠几秒钟或执行第一个请求,然后执行第二个一。 这是我的代码
public ngOnInit(): void {
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]);
}
});
this._service.getDept().subscribe(depts => {
for (var i=0; i<depts.length; i++)
console.log(depts[i])
for (var name in depts[i]) {
this.depts.push(depts[i][name]);
}
});
}
有什么解决办法吗?
您可以在收到 getPlant() 的响应后 运行 getDept()。
public ngOnInit(): void {
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]);
}
this._service.getDept().subscribe(depts => {
for (var i = 0; i < depts.length; i++)
console.log(depts[i])
for (var name in depts[i]) {
this.depts.push(depts[i][name]);
}
});
});
}