Angular-RxJS | Sample Angular application that uses RxJS | Reactive Programming library
kandi X-RAY | Angular-RxJS Summary
kandi X-RAY | Angular-RxJS Summary
Sample Angular application that uses RxJS for reactive development. Find the associated Pluralsight course here:
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of Angular-RxJS
Angular-RxJS Key Features
Angular-RxJS Examples and Code Snippets
Community Discussions
Trending Discussions on Angular-RxJS
QUESTION
I'm going through Angular's tutorial at https://angular.io/tutorial/toh-pt6 and I've got a couple of questions. My second question is very close to one asked here about the same Angular demo, but though it almost gets to my specific syntax question, it falls just short, and the one response doesn't get into it.
Retrieving data from a service from an Observable returned by a call to HttpClient.get
, they have a catchError
operator in the pipe:
ANSWER
Answered 2020-Dec-11 at 19:31I figured it out. But this is one of the most confusing syntax details I've ever encountered. If it had been written
QUESTION
I am having trouble getting the sum (or any reduction) of an inner number property of an Observable within another Observable.
I have an Observable array of "Account" objects (Observable
).
ANSWER
Answered 2020-Jul-21 at 20:24Well - first I don't think you should be using obsevables like that.
If you only need to the totalBalance
you could use something like this (:
QUESTION
I am working on an Angular 9, RxJS 6 app and have a question regarding the different outcomes of piping subject values and doing unit conversion in that pipe.
Please have a look at this stackblitz. There, inside the backend.service.ts
file, an observable is created that does some "unit conversion" and returns everything that is emmitted to the _commodities Subject. If you look at the convertCommodityUnits
function, please notice that I commented out the working example and instead have the way I solved it initially.
My question: When you use the unsubscribe buttons on the screen and subscribe again, when using the "conversion solution" that just overrides the object without making a copy, the values in the HTML are converted multiple times, so the pipe does not use the original data that the subject provides. If you use the other code, so creating a clone of the commodity object inside convertCommodityUnits
, it works like expected.
Now, I don't understand why the two ways of converting the data behave so differently. I get that one manipulates the data directly, because js does Call by sharing and one returns a new object. But the object that is passed to the convertCommodityUnits
function is created by the array.prototype.map
function, so it should not overwrite anything, right? I expect that RxJS uses the original, last data that was emitted to the subject to pass into the pipe/map operators, but that does not seem to be the case in the example, right?
How/Why are the values converted multiple times here?
This is more or less a follow-up question on this: Angular/RxJS update piped subject manually (even if no data changed), "unit conversion in rxjs pipe", so it's the same setup.
...ANSWER
Answered 2020-May-12 at 08:36When you're using map
you got a new reference for the array. But you don't get new objects in the newly generated array (shallow copy of the array), so you're mutating the data inside the element.
In the destructuring solution, because you have only primitive types in each object in the array, you kind of generate completely brand new elements to your array each time the conversion method is called (this is important: not only a new array but also new elements in the array => you have performed a deep copy of the array). So you don't accumulate successively the values in each subscription.
It doesn't mean that the 1-level destructuring solution like you used in the provided stackblitz demo will work in all cases. I've seen this mistake being made a lot out there, particularly in redux pattern frameworks that need you to not mutate the stored data, like ngrx, ngxs etc. If you had complex objects in your array, the 1-level destructuring would've kept untouched all the embedded objects in each element of the array. I think it's easier to describe this behavior with examples:
QUESTION
I want to pass URL besides getting its response data in next pipable operator of RxJS. What do you think is the smartest way to achieve this? Thanks in advance.
Here is an example. https://stackblitz.com/edit/angular-rxjs-passing-data-to-next-operator-question
I tried some operators, but I couldn't find right one.
(Actually, I don't even know why passing function which returns observable to mergeMap
results in getting data as parameter of function in next operator...)
ANSWER
Answered 2019-May-08 at 14:11You can map the response to whatever you want:
QUESTION
I'm using an image API to pull some image information and I want to create an array with unique author names for each image returned. I'm using the distinct
RxJS to try to do this, but right now the entire objects are being returned.
Here's what the API data comes back looking like:
...ANSWER
Answered 2020-Apr-05 at 18:12this.http.get will emit array of values - not each value separately. That's why distinct is not valid here. But you can convert array of values to emit each value separately with mergeAll operator
const authors$ = this.http.get(apiURI).pipe( mergeAll(), distinct((item: Image) => item.author) );
QUESTION
I am working on angular app where I am able to sort data in ascending and descending order.Though everything is working fine I want to optimise my code so that I don't repeat myself and write an efficient code.
Here is my sample data in console sample data in console
I have array of object where I want to sort data in ascending and descending order of onclick.Everytime I am calling a function for every field.Is there a better way to do it
Here is my code
data.ts
...ANSWER
Answered 2020-Apr-02 at 07:45Maybe you can use something like this. Pass the filed also that you want to sort on.
I haven't handled what the value of isAscending should be. I think you can figure that out.
QUESTION
I've been looking around for a simple way to avoid the memory leaks I've read about caused by failing to unsubscribe. Most of the time I'm just wanting the ONE response from my backend. And then I'll want to unsubscribe. So why not call it in the callback?
...ANSWER
Answered 2018-Nov-08 at 21:53Unsubscribing is important to avoid memory leaks, but it maybe not so important if you are "just wanting the ONE response from backend".
What I mean is that if, behind your this.puzzleService.login
you are using the Angular http
service, than you do not have to worry about unsubscribing since the service itself unsubscribes as soon as it gets the response from the backend, or an error occurs.
Unsubcribing is important when you face Observable streams which emit, by their nature, more than one value, and this is not the case of an http call but may be the case of web-sockets streams or other kind of streams.
This article from Ben Lesh, top contributor if not lead of RxJS, casts some light on the topic.
QUESTION
I have working angular code which merges two API results where the second call is dependent on the first. It's a simple join operation:
...ANSWER
Answered 2020-Jan-05 at 13:47Here for the rescue:
you are very close with your solution. The only problem is that you didn't take both returns. I would do something more in the line of (not exact, because I'm not 100% sure what you are going for):
QUESTION
I came across this answer Angular/RxJs When should I unsubscribe from `Subscription` on how to stop a subscription using takeUntil(). The answer also states that
The secret sauce (as noted already by @metamaker) is to call .takeUntil(this.ngUnsubscribe) before each of our .subscribe() calls which will guarantee all subscriptions will be cleaned up when the component is destroyed.
I am however having problems understanding why one of my two examples does not stop the subscription. Here is a working example
My Service:
...ANSWER
Answered 2018-Feb-20 at 10:58this.gps = Observable.timer(3000, 1000);
QUESTION
I have two observables. First observable is used for ngIf-Block Statement. Second observable is used inside the ngIf-Block and maps values from first observable.
The second observable do not get the first update from source inside the ngIf-Block. On otherhand, outside of ngIf-Block it works.
Any explanations for this unexpected behavior?
Example: https://stackblitz.com/edit/angular-rxjs-template-update-problem
...ANSWER
Answered 2019-Nov-08 at 11:39This is happening because number$
is Subject and it's used inside *ngIf
which means it subscribes to sum$
only after this template block is created and that happens after sum$
is updated with this.number$.next(1)
.
So what you can do is using ReplaySubject(1)
instead that will replay its latest item to every new subscriber so when the template async
pipe inside *ngIf
subscribes to it it gets the last value replayd and updates as you expect.
Your updated demo: https://stackblitz.com/edit/angular-rxjs-template-update-problem-xb5f3s?file=src/app/app.component.ts
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install Angular-RxJS
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page