【发布时间】:2017-06-21 21:50:39
【问题描述】:
我正在通过 Get 请求在 API 中查找数据,我需要 OnInit 中的数据用于组合其他数据。问题是该方法正在被调用,但它是一个异步方法(没有等待),它到处传递,但是当获得返回时,主方法的执行已经完成,没有结果。我尝试了异步方法的实现,但没有解决。
服务:
getObjects(): MyClass[] {
let objects = new Array<MyClass>();
this.obterTodos<MyClass>(this.uriApi)
.map(res => res.map(x => new MyClass(x.Description, x.Id)))
.subscribe(entities => {
objects = entities;
});
return objects ;
}
获取请求
public getObjects<TEntity>(url: string): Observable<TEntity[]> {
return this.httpService
.get(this.serviceUrl + url, this.options)
.map(res => res.json())
.catch(this.handleError);
}
组件:
ngOnInit() {
this.myObjects= this.setorService.obterSetores();
console.log('this.myObjects is empty here');
}
【问题讨论】:
标签: json angular asynchronous get