【发布时间】:2018-04-13 08:46:02
【问题描述】:
我一直在通过 get 请求获取对象。
我在component.ts中有如下函数
private _getPolygonById(id: number) {
let poly: any;
this.dataService.getCollectionbyId(id).subscribe(data => {
const res = data.json();
poly = JSON.parse(res.string);
});
return poly;
}
我的 service.ts 包含以下内容:
getCollectionbyId(id: number) {
console.log("launching request");
return this.http.get('api/project-collections/'+id);
}
问题是 poly 未定义并按原样返回,响应被异步处理。我怎样才能等待请求,直到它得到响应,然后将它影响到 poly,返回它?
【问题讨论】:
标签: angular angular4-httpclient