我刚开始使用Angular 2中的Observables并有一个问题。
我的数据服务类中有一个名为getExplorerPageData的方法,该方法进行http调用,返回带有几个数组的数据结构。我想有一个名为getTag的附加函数,它可以检索在getExplorerPageData()调用中检索到的一个项目。
要明确的是,当我调用getTag时,我不想再次点击服务器,我宁愿只是从已经调用getExplorerPageData()的调用中检索该项目。
我想知道最好的方法是什么?
setPixel()
答案 0 :(得分:0)
You can add the call to the .map(...)
operator or use the do(...)
operator:
getExplorerPageData(): Observable<IExplorerPageData> {
return this.http.get(this.baseurl + "/explorerpagedata")
.map((response: Response) => {
var data = <IExplorerPageData>response.json();
getTag(data);
return data;
})
.catch(this.handleError);
}
or
getExplorerPageData(): Observable<IExplorerPageData> {
return this.http.get(this.baseurl + "/explorerpagedata")
.map((response: Response) => <IExplorerPageData>response.json())
.do((data) => getTag(data))
.catch(this.handleError);
}
With the map()
operator it's important to return the value at the end, because the returned value is what the subscriber receives.
With the do()
operator the return value doesn't matter, the value returned from the previous map()
is forwarded to the subscriber, no matter what do()
returns.