【发布时间】:2019-03-24 15:25:07
【问题描述】:
我正在尝试使用 Angular 实现一个测试应用程序。问题场景是我需要执行 Get 并根据从该 Get 请求中获得的数据返回一个值。见下文:
public getNextId(entityName: string): number {
console.info('Retrieving next ID for ' + entityName);
let seqId: SeqId;
const url = `${this.apiURL}/${entityName}`;
console.info('SeqID URL: ' + url);
this.http.get<SeqId>(url)
.subscribe((data: SeqId) => seqId = data); -> 1
/*Do something with seqId */ -> 2
return seqId.nextEntityId;
}
问题是 (2) 在 (1) 之前执行。当达到 (2) 时,seqId 变量尚未设置。
如何使用相同的方法进行 Get、处理检索到的数据并返回某些内容?
提前致谢
【问题讨论】:
-
您必须在
subscribe回调中进行处理。
标签: angular typescript observable httpclient subscribe