rxjs

What is pipe() function in Angular 2?

房东的猫 提交于 2019-12-20 08:05:27
问题 Pipes are filters for transforming data (formats) in the template. I came across the pipe() function as below. What does this pipe() function exactly mean in this case? return this.http.get<Hero>(url) .pipe( tap(_ => this.log(`fetched hero id=${id}`)), catchError(this.handleError<Hero>(`getHero id=${id}`)) ); 回答1: Don't get confused with the concepts of Angular and RxJS We have pipes concept in Angular and pipe() function in RxJS. 1) Pipes in Angular : A pipe takes in data as input and

Angular4 Websocket rxjs Reconnect and onError

亡梦爱人 提交于 2019-12-20 06:29:57
问题 It looks like there are several similar questions but after several days I do not find the proper answer. My question is how to know if the server has closed the websocket and how to try to reconnect. I have seen several examples but none of them worked properly when I wanted to implement the fonctionality of closing the websocket from the client when I change the view. Then I found this example which it's the best one I have seen so far, and with a small modifications I was able to add a

Rxjs - DistinctUntilChanged() with keyup event

余生长醉 提交于 2019-12-20 06:29:50
问题 I'm using Rxjs 6 which filters an observable returned by Firebase based on (keyup) event in a form field. I have an issue when the user keeps pressing backspace while there is no value in the form field, then it looks like the observable is constantly refreshed. Adding a pipe with DistinctUntilChanged() doesn't seem to be effective: Typescript Filter Function: updateFilter2() { const val = this.filteredValue.toLowerCase().toString().trim(); if (this.filteredValue) { this.loadingIndicator =

How to finalize nested Observables?

一笑奈何 提交于 2019-12-20 06:26:26
问题 Please, take a look at the following try for better understanding: this.loading = true; obs.pipe( finalize(() => this.loading = false) //If set to false here and the person has children then there will be another request and the requests are not over yet ).subscribe(person => { if(person && person.children){ obs2.pipe( finalize(() => this.loading = false) //If only set to false here and the person has no children then the false property will never actually be set to false ).subscribe(children

Angular 7: How to re-write nested subscribtions?

会有一股神秘感。 提交于 2019-12-20 06:12:25
问题 I have a component that displays a filtered list of items. It is subscribed to two observables - the first one is for (filter) parameters that need to be passed into the second observable to get the filtered list of items. public filteredItems = []; this.myService.getFilterParams() .subscribe(params => { this.myService.getFilteredItems(params) .subscribe(items => { this.filteredItems = items}); }); I've read that chaining subscribtion is not the best practice (the code works fine otherwise),

Wait for another observable before proceeding

旧街凉风 提交于 2019-12-20 05:16:22
问题 I don't know if I should post any code for this. But I will if required. I have an Angular2 directive, MyDirective and a service MyService The service makes an http call in its constructor to fetch something and store in say this.list . Directive injects the service and uses it as myService.isPresent(item) . Now, the problem is that by the time directive executes, the http call for fetching the list in the service isn't completed. Is there a clean way to make the directive wait till the

rxJS observable not reaching subscribe

谁说我不能喝 提交于 2019-12-20 05:02:52
问题 I'm with Angular 2 and RxJS and I'm having a hard time setting up a simple observables system. As far as I understand, operator do is used for side effects, and you place the code to deal with the results returned by the observable in the susbcribe() function. So my Component ask the Service to initialize the system. The Service makes 2 http calls to the server and combines them both in a single stream for the component to subscribe and make sure everything is ready. One of the 2 http calls

rxJS observable not reaching subscribe

这一生的挚爱 提交于 2019-12-20 05:01:22
问题 I'm with Angular 2 and RxJS and I'm having a hard time setting up a simple observables system. As far as I understand, operator do is used for side effects, and you place the code to deal with the results returned by the observable in the susbcribe() function. So my Component ask the Service to initialize the system. The Service makes 2 http calls to the server and combines them both in a single stream for the component to subscribe and make sure everything is ready. One of the 2 http calls

How to implement nested secondary routing and still get the data from child? Angular 6

白昼怎懂夜的黑 提交于 2019-12-20 04:53:34
问题 My goal is to send data from a routed child to the parent. I want to get the reference of a variable from the child, which is nested within a child to the parent through secondary routes so that the variable is synced to the parent. This variable will eventually be the boolean of a form validation state. Unfortunately, when I use Behavior Subject I think I have to use the component selector which seems not to play nicely with the routes in my application. This is a sample from the template of

Angular interval pipe with a longer task inside

我只是一个虾纸丫 提交于 2019-12-20 04:38:37
问题 I have this code inside my component: ngOnInit() { ... this.counterValue$ = interval(1000).pipe( switchMap(() => this.perfService.getCounter(this.counterUrl)), map(i => this.updateChart(i)), ); this.counterValue$.subscribe(v => console.log(v)); } I wrote this to update a chart every 1s. The problem is that the perfService.getCounter() is taking more than 1s to return. And this causes the following http requests to get canceled: how to fix this? 回答1: If you want to update it every 1s while