我有一个角度应用程序,在这个应用程序中,我使用RxJ库从后端API服务器获取数据。电话看起来是这样的:
allPowerPlants(onlyActive: boolean = false, page: number = 1): PowerPlant[] {
const self = this;
const path = `/powerPlants?onlyActive=${onlyActive}&page=${page}`;
this.apiService.get(path).subscribe(
powerplants => {
powerplants.map(item => {
if (this.isPowerPlant(item)) {
// make the item an instance of PowerPlant
self.powerPlants.push(item as PowerPlant);
}
});
},
err => {
console.log('oops **** some error happened');
// handle error
});
return this.powerPlants;
}我现在有以下问题:
powerPlants是包含此方法的类中的一个数组!
发布于 2017-08-29 15:38:00
下面是你问题的答案。
您可以使用Observable.map,以防您想要发送数据--其他人可以订阅它,
allPowerPlants(onlyActive: boolean = false, page: number = 1): PowerPlant[] {
const self = this;
const path = `/powerPlants?onlyActive=${onlyActive}&page=${page}`;
this.apiService.get(path).map(
powerplants => {
powerplants.map(item => {
if (this.isPowerPlant(item)) {
// make the item an instance of PowerPlant
self.powerPlants.push(item as PowerPlant);
}
});
},
err => {
console.log('oops **** some error happened');
// handle error
});
return this.powerPlants;
}https://stackoverflow.com/questions/45942875
复制相似问题