【发布时间】:2017-01-19 18:29:14
【问题描述】:
在组件中,我们订阅了来自服务的两个 http 返回结果,并且在服务中,两个 http 调用返回了可观察的。
在组件打字稿中
catalogPromise = this.ondemandService.getVodCatalog().subscribe((data) => {
console.log(data);
});
服务中:-
getVodCatalog(url,user){
return this.getVodCategories().map((data) => {
let result,result2;
let Observers = [],
cats = [],
catalogs = [];
result=data.json();
result.categorylist.forEach((catergory) => {
cats.push(catergory.name);
Observers.push(this.getCategoriesCatolog(catergory.id,{}));
});
console.log(Observers);
Observable.forkJoin(Observers).subscribe((vodCatalogs)=> {
vodCatalogs.forEach((vodeCatalog,idx)=>{
let v1 = vodeCatalog.json();
if(v1.vodlist)
{
catalogs.push({
label: cats[idx],
list: v1.vodlist
});
}
});
return catalogs;
});
})
.catch(this.handleError);
}
getVodCategories(){
let url="http://CategoryList";
let headers = new Headers({ 'Content-Type': 'application/json','Accept':'application/json, text/plain, */*'});
let body = {
offset: 0,
count: -1,
type: 'dfasd',
categoryid: -1,
};
let options = new RequestOptions({
headers: headers,
url:url,
withCredentials: true,
timeout:30000,
body:body
});
return this._http.post(url,body,options);
}
getCategoriesCatolog(cateogoryId,options){
var url="http://VodList";
let headers = new Headers({ 'Content-Type': 'application/json','Accept':'application/json, text/plain, */*' });
let body = {
count: 100,
offset: 0,
orderType: 0,
categoryid: cateogoryId,
};
options = new RequestOptions({
url:url,
withCredentials: true,
timeout:30000,
body:body
});
return this._http.post(url,body,options);
}
在服务中,getVodCategories() 和 getCategoriesCatolog() 返回 observable 结果,但是当我们尝试在组件中订阅它时,整个数据未定义,之后 Observable.forkjoin() 正在运行。
【问题讨论】:
标签: angularjs angular typescript ecmascript-6 rxjs