【发布时间】:2019-06-29 23:13:17
【问题描述】:
我正在尝试构建应用程序,我正在发送一个获取请求以从组件获取数据。该请求将被调用,并且数据将用于执行下一步。但是,在我得到服务响应之前,它正在执行下一步。
这是我的服务文件:
getSabhaListDetailsName(sabhaMandalName: string): Observable<any> {
const sabhaListDetailsNameURL = this.rooturl + 'sabhatypebysabhaname';
return this.http
.post(sabhaListDetailsNameURL,{}, { params: { sabha_name: sabhaMandalName } })
.pipe(map(this.extractData),
catchError(this.handleError<any>('sabhaListDetailsNameURL')));
}
这是我的组件文件,我从中调用服务,
this.sabhaService.getSabhaListDetailsName(element.sabha_id).subscribe(result => {
this.tempRetrieveSabha = result.data.sabhaList;
//return this.tempRetrieveSabha;
// });
console.log(this.tempRetrieveSabha);
this.tempRetrieveSabha.forEach(
element1 => {
console.log(this.tempRetrieveSabha);
if (element1.sabha_type == element.sabha_type) {
this.tempSabha_ID = element1.id;
this.tempSabha_Type = element1.sabha_type;
this.tempFollowup_ID = element.followup_id;
}
})
});
如何在遍历数组之前等待 http 请求完成。
【问题讨论】:
-
你可以试试
async await -
在订阅中移动 foreach 部分代码。
标签: javascript angular typescript angular6 angular-httpclient