我需要多次调用服务函数。 我需要等待每次调用的响应并对返回的数据进行一些验证。 我之前使用过switchmap,但是当我知道需要拨打多少电话时,例如1:获取用户,2:获取用户详细信息。
但是现在我不知道我需要拨多少个电话,它的动态,它可以是1次,它叫10次。 关于如何做到这一点的任何想法?
嗨,谢谢你的例子。但是,如果我需要循环服务调用,我从订阅中执行此操作? 我是否需要在服务函数中使用observer.next?
我的服务功能如下:
getNavigation(tree: Navigation): Observable<Navigation[]> {
return this.partfinderService.getNavigation(tree.id);
}
这样称呼:
this.getNavigation(this.master)
.expand((data: Navigation[]) => {
return this.getNavigation(data[0]);
}).subscribe(context => contextData = context);
答案 0 :(得分:0)
感觉就像你真正需要的是为你的角度应用添加ngrx商店支持。 Here is where you can start如果你有时间改进和重构。
根据Supamiu的建议,扩展会做到这一点,我做了一个简单的stackblitz示例here。
服务功能:
doSomething(context: any) {
return Observable.create(function (observer) {
setTimeout(() => {
observer.next(
{
run: context.run + 1,
value : context.value + '- Run ' + context.run
});
}, 1000);});
}
订阅:
this._testService.doSomething(this.context)
.pipe(
expand(context => {
return this._testService.doSomething(context)
// Condition of the last run
.pipe(filter(c => c.run <= 5));
})
).subscribe(context => this.context = context);