【发布时间】:2019-07-19 00:48:27
【问题描述】:
我需要从 Angular 对我的数据库执行一系列 http 请求。在第一步中,我得到一个对象数组。这工作正常。 在第二步中,我想用附加数据填充每个对象。所以我需要等待第一个 http.request 完成。 我也不想在我的数据被提取之前返回。
我的函数 getData() 应该返回一个 Observable,因为接口/签名不应该改变(因为它正在程序的其他部分使用,并且 observable 对程序逻辑有意义)。
// Returns an array with objects, that have an id, that is needed for the second call
data[] : Object = new Array();
populatedData[] : Object = new Array();
getData(): Observable<Object[]>{
this.http.get<Object[]>("http://localhost:3000/data").subscribe(data => this.data = data);
// This needs the data from the first call
for(int i = 0; i < data.length; i++){
this.http.get<Object>("http://localhost:3000/data/"+data[i].id).subscribe(data => populatedData.push(data));
}
return of(populatedData);
}
【问题讨论】:
标签: javascript angular typescript