[英]Angular2 - nested http requests
我有一些看起来像这样的组件代码:
private myThing: MyThing;
ngOnInit() {
this.service.getMyThing().subscribe(thing => {
this.myThing = thing;
}
}
但是,服务看起来像这样-它由两个http请求连接的数据组成。 第一个来自“ otherService”,它为Thing对象获取一个附加属性,第二个调用则获取基本的Thing对象。 我想结合这些结果,并在所有动作完成时提醒订阅者,即this.myThing应该具有“ .prop”属性...
public getMyThing(): Observable<MyThing> {
this.otherService.getThingProperty().subscribe(prop => {
return this.http.get('url')
.map(res => res.json())
.map(thing => {
thing.prop = prop;
});
});
}
但这显然是错误的-函数认为这是有道理的……我只是不知道如何让组件订阅最终的可观察对象。 任何提示表示赞赏。 谢谢!
尝试switchMap
:
public getMyThing(): Observable<MyThing> {
return this.otherService.getThingProperty().switchMap(prop => {
return this.http.get('url')
.map(res => res.json())
.map(thing => {
thing.prop = prop;
});
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.