大佬教程收集整理的这篇文章主要介绍了angular – Observable继续调用API并根据条件更改参数,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
所以流程如下:
>将GET请求设为查询参数reqMode =”:
>使用包含值或完成的reqMode的最后一个数组检索响应.
>如果我收到一个值,那么我需要发出相同的请求,但发送带有值的reqMode参数.
>如果我收到了,那么我将停止并返回自第一次通话以来的所有记录.
我在正常订阅时得到第一组值,但这是我在阅读文档后的尝试,但它没有意义:
getRecords(){ let url = thi@R_772_11163@rl + 'reqMode='; return this.http.get(url) .doWhile() //what would I do here }
当尝试使用类型为Observable< response>的Observable执行.doWhile时.我正在寻找使用Observables的任何替代方案,以满足我的需求.
我使用concatMap()
并递归调用自己,直到reqMode eqaul为“done”:
观看现场演示:http://plnkr.co/edit/w0DdepslTaKrLSB3aIkA
import {Observable,Subject} from 'rxjs'; const result = new Subject(); const closeBuffer = new Subject(); const buffer = result.buffer(closeBuffer.asObservable()); function sendhttprequest(reqModE) { return Observable.of('{"reqMode":' + reqMode + '}') .map(response => JSON.parse(responsE)) .concatMap(data => { console.log('http Response:',data); // Add data to the buffer of results result.next(data); if (data.reqMode == 'done') { // Return an empty value wrapped as an Observable so concatMap can work // with it and emit onNext when it completes (which is immediately // thanks to the `.of()` operator). return Observable.of(null); } else { // Simulate that the next call returns 'done' return sendhttprequest('"done"'); // Uncomment this for real usage //return sendhttprequest(data.reqModE); } }); } // Subscribe to the buffer where I'll receive the value. buffer.subscribe(val => console.log('Next: ',val)); // Simulate http request with reqMode = 42 sendhttprequest(42).subscribe(() => { console.log('done'); // Emit values from the buffer. closeBuffer.next(null); closeBuffer.complete(); });
我使用of()
运算符来模拟请求并返回包含为Observable的值.我还使用Subject来保存使用buffer()
运算符缓冲的所有响应.我订阅缓冲区以获取最终的响应数组(如果将此代码包装到函数中,您很可能会返回缓冲区,您可以在以后订阅).
回应如下:
http Response: Object {reqMode: 42} http Response: Object {reqMode: "done"} Next: [Object,Object]
看到类似的问题:Angular 2 + rxjs – how return stream of objects fetched with several subsequent http requests
以上是大佬教程为你收集整理的angular – Observable继续调用API并根据条件更改参数全部内容,希望文章能够帮你解决angular – Observable继续调用API并根据条件更改参数所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。