rxjs

Wait for subscription in constructor of a Service

假装没事ソ 提交于 2019-12-23 23:04:14
问题 In my service, I want to wait for until local variable baseurl is not initialized before making another http request. Below is my service code: @Injectable() export class CoursesService { baseUrl; constructor(private http: Http) { if(this.baseUrl != undefined){ this.getJSON().subscribe(data => this.baseUrl=data, error => console.log(error) ); } } public getJSON(): Observable<any> { return this.http.get("assets/apiDetails.json") .map((res:any) => res.json()); } getCourses(){ return this.http

Preventing a Pyramid of Doom using rxjs .subscribe with Angular - flattening the number of .subscribes

China☆狼群 提交于 2019-12-23 22:30:18
问题 I'm currently looking into RxJS's .merge however I'll also ask the question here as I find explanations here at times to be brilliant. Okay, I have a form that depending on user input opens a modal window, I subscribe to the modal close event and pass back some data that I will use after I call / subscribe to a service method to retrieve some data, then when this has happened I do the same again and call / subscribe another service method to update some date then when this has finished I run

RxJS subscribe inside subscribe with actions on error

爱⌒轻易说出口 提交于 2019-12-23 22:20:09
问题 I have code with subscribe inside subscribe : this.http.post('/update1', newData).subscribe( (response) => { const r = response.json; const id = r['id']; this.http.post('/update2?id=' + id, newData).subscribe( () => { this.http.post('/update3?id=' + id, newData).subscribe( () => { console.log('success'); }, () => { // revert changes made to update2 this.http.post('/update2', previousValues).subscribe(); // revert changes made to update1 this.http.post('/update1', previousValues).subscribe();

Persistent subscriptions with rxjs in Angular 5

筅森魡賤 提交于 2019-12-23 22:09:39
问题 I'm still a bit new to rxjs in Angular 5 and it a bit hard to formulate my question. Still I hope for some tips. I often end up with the same setup: Multiple Components to display the same data A single Service to access the data Now I have 2 options when receiving data via Observables: a) Subscribe to an observable to get data once, and subscribe again to get updates b) Subscribe to an observable and always get updates when data changes a) ist straight forward, but with b) I often have

Angular TS Error TS1109

[亡魂溺海] 提交于 2019-12-23 19:45:55
问题 i get following error in console: webpack: Compiling... 10% building modules 0/1 modules 1 active ...p/shoppinglist2/src/app/app.module.tsERROR in src/app/meal.service.ts(46,56): error TS1109: Expression expected. Date: 2017-12-31T09:55:50.224Z Hash: 8003d6d8f334085afa7f Time: 267ms chunk {inline} inline.bundle.js (inline) 5.79 kB [entry] chunk {main} main.bundle.js (main) 73.1 kB [initial] [rendered] chunk {polyfills} polyfills.bundle.js (polyfills) 558 kB [initial] chunk {styles} styles

Rxjs 6: using catchError() gives You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable

瘦欲@ 提交于 2019-12-23 19:36:08
问题 I'm using the new syntax to comply with RxJS 6 pipe() . Also using Angular 6. I have a service that handles http requests, and it pipes map and catchError to display a toast in case of connection error. Nevertheless, If I add catchError I get in console You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable. getDataHttpRequest(url, returnType) { return this.http.get(url, this.getRequestOptions()) .pipe( map((response) => { if(response)

彻底理解RxJS里面的Observable 、Observer 、Subject

99封情书 提交于 2019-12-23 19:29:30
最近闲来无事,常常重读Angular官方文档,颇能发现些有趣的地方。让我想起海澜之家的广告词:每次都有新体验。 关于RXJS的基础概念, observable 和 observer ,我们好多次搞得头晕眼花。 其实,看下面这简简单单的一行代码就懂了它们的关系: observable.subscribe(observer); observable 是数据源头,是生产者,是待订阅者,通过subscribe方法可以被订阅,而 observer 是消费者,是数据使用者,是订阅者。 observer其实是一个有三个回调函数的对象,每个回调函数对应一种 Observable 发送的通知类型( next , error , complete )。回调函数不必每次都提供三个。如果我们只提供了一个回调函数作为参数,subscribe会将我们提供的函数参数作为next的回调处理函数。 observable. subscribe() 是一个Subscription对象。 Subscription 就是表示Observable 的执行,可以被清理。这个对象最常用的方法就是unsubscribe方法。同时,它还有 add方法可以使我们取消多个订阅。 我有一个比喻可以很好的理解这种订阅关系:现在有一家牛奶生产商,它们家的牛奶质优价廉,鲜美醉人。它在电视上发布广告,所有人都可以打它们的电话订奶。这个时候

Angular2 - *ngIf and async observables

爷,独闯天下 提交于 2019-12-23 18:16:53
问题 I have problem with using *ngIf with observable variables. The thing is that when I hide element with *ngIf , and show it again, values won't load so: <div *ngIf="showDiv"> {{ someObservable$ | async }} </div> Basically when showDiv is set to true at the first place, the someObservable loads, but when I set it to false and then again to true , value won't load. What's wrong? Regards 回答1: UPDATE: Thanks to j2L4e, for his hint! BehaviorSubject is the key! See this plunker: https://plnkr.co/edit

How to recover from errors in rxjs?

烂漫一生 提交于 2019-12-23 17:58:43
问题 I'm trying to understand how to consume observable sequences and how to recover from errors. My example is a bit contrived but my real implementation is a bit too complex to show here. Anyway, I have someObservable that emits values when the user clicks in the UI. This should trigger a request to the API (GET/POST/etc). The problem is that if the API returns an error, postData isn't called anymore after that. I've make an example here that shows the problem. I've found that I can use Rx

Why button click trigger is different from setTimeout() trigger?

二次信任 提交于 2019-12-23 17:57:57
问题 Consider the following, almost identical, two snippets. The difference is: the first one uses setTimeout() to trigger the event the second one triggers the event when the button is clicked If you check the console, you'll see that the last two lines in Snippet 1 are: App rendering 1 folder(s) Observed js and in Snippet 2 are: Observed js App rendering 1 folder(s) Question: Why is the order reversed? setTimeout() playground Button playground Snippet 1: setTimeout() trigger class App extends