【发布时间】:2017-08-27 05:34:59
【问题描述】:
我试图通过使用forkjoin 来避免嵌套的 observables。当前(嵌套)版本如下所示:
this.http.get('https://testdb1.firebaseio.com/.json').map(res => res.json()).subscribe(data_changes => {
this.http.get('https://testdb2.firebaseio.com/.json').map(res => res.json()).subscribe(data_all => {
/* Do this once resolved */
this.platform.ready().then(() => {
this.storage.set('data_changes', data_changes);
this.storage.set('data_all', data_all);
document.getElementById("chart").innerHTML = "";
this.createChart();
});
});
},
err => {
this.platform.ready().then(() => {
console.log("server error 2");
document.getElementById("chart").innerHTML = "";
this.createChart();
});
});
}
我可以将第一部分改写为:
Observable.forkJoin(
this.http.get('https://testdb1.firebaseio.com/.json').map((res: Response) => res.json()),
this.http.get('https://testdb2.firebaseio.com/.json').map((res: Response) => res.json())
)
但我不确定如何添加.subscribe 方法来访问data_changes 和data_all。
看看另一个例子,我知道它应该看起来像.subscribe(res => this.combined = {friends:res[0].friends, customer:res[1]});,但我不确定如何将它适应我的例子。
【问题讨论】:
标签: angular typescript ionic2 observable