rxjs

Combine two or more (boolean) observables on single ngIf using async pipe

淺唱寂寞╮ 提交于 2020-06-10 09:08:51
问题 Without observables I can write the following line in the HTML template: <div *ngIf="(myVarA || myVarB) && myVarC !== x"></div> How do I translate this line if all myVar variables are now actually observables? <div *ngIf="((myVarA | async) || (myVarB | async)) && (myVarC | async) !== x"> does not work. In another question (Putting two async subscriptions in one Angular *ngIf statement) the possibility to combine two or more observables into one ngIf can be achieved like <div *ngIf="{ a:

How to unit test unsubscribe function in angular

[亡魂溺海] 提交于 2020-06-10 02:52:50
问题 I would like to find a way to test unsubscribe function calls on Subscriptions and Subjects. I came up with a few possible solutions, but every one of these have pros and cons. Please keep in mind that I do not want to alter the access modifier of a variable for testing purposes. Accessing private variable of component with reflection. In that case I have a private class variable which stores a subscription: component.ts: private mySubscription: Subscription; //... ngOnInit(): void { this

How does throttleTime operator's config parameter work? (ThrottleConfig)

醉酒当歌 提交于 2020-06-08 18:24:05
问题 I have read the throttleTime documentation, but I don't get the operator fully. I know how throttleTime(1000) works. After an event arrives it will skip all subsequent events for 1 second and then start this process again. What I have trouble to understand is how exactly ThrottleConfig works, which is the third parameter of the operator. throttleTime<T>( duration: number, scheduler: SchedulerLike = async, config: ThrottleConfig = defaultThrottleConfig): MonoTypeOperatorFunction<T> How do

RxJS - Multiple Requests using forEach and waiting all to finish

大兔子大兔子 提交于 2020-06-01 12:41:14
问题 I have this case: First i call a service, and get a list of items. (Array of objects) For each item in this list, i call another service, and they all actually can be fired parallel. I have to wait for all the answers. When i have all the answers, i have a finally logic. I now have sth like this without using RxJS properly: this.service.readArray().subscribe((array: Object[]) => { if (array.length > 0) { array.forEach((item, index) => { this.service2.readItem(item.id) .subscribe(details => {

RxJS - Multiple Requests using forEach and waiting all to finish

生来就可爱ヽ(ⅴ<●) 提交于 2020-06-01 12:39:30
问题 I have this case: First i call a service, and get a list of items. (Array of objects) For each item in this list, i call another service, and they all actually can be fired parallel. I have to wait for all the answers. When i have all the answers, i have a finally logic. I now have sth like this without using RxJS properly: this.service.readArray().subscribe((array: Object[]) => { if (array.length > 0) { array.forEach((item, index) => { this.service2.readItem(item.id) .subscribe(details => {

RxJS - Multiple Requests using forEach and waiting all to finish

左心房为你撑大大i 提交于 2020-06-01 12:37:21
问题 I have this case: First i call a service, and get a list of items. (Array of objects) For each item in this list, i call another service, and they all actually can be fired parallel. I have to wait for all the answers. When i have all the answers, i have a finally logic. I now have sth like this without using RxJS properly: this.service.readArray().subscribe((array: Object[]) => { if (array.length > 0) { array.forEach((item, index) => { this.service2.readItem(item.id) .subscribe(details => {

¿How exactly does the mergeMap operator work and in which cases is it used?

倖福魔咒の 提交于 2020-06-01 06:07:31
问题 Before coming here I have read the official documentation of Rxjs and some other pages but I am still not clear. What I understood is this: It is used to "join" 2 observables and thus obtain a single observable as a result, I also saw that it is used to "flatten" an observable (I am also not very clear). Now ... I have days trying to program a user registry using Angular and Node.js with Express and I found a little tutorial which I decided to use and it has this code: import { Injectable,

How to use withLatestFrom with a selector in ngrx?

吃可爱长大的小学妹 提交于 2020-05-30 11:37:42
问题 I have ngrx application, and I can't get the value from a selector in the effect, by the payload I sent in the component. I write an example code of what I deal with: This is my state, a simple docs array in it. export const initialState = { docs: [{ id: 1, name: "doc1" }, { id: 2, name: "doc2" }] }; export const reducer = createReducer( initialState, ); I dont handle any action for this questions, this is not the issue. In my component I get the array using a selector: docs$ = this.store

timeout inside a subscription rxjs

ぐ巨炮叔叔 提交于 2020-05-30 08:46:28
问题 I have the following code this.someFormGroup.controls['control1'].valueChanges.subscribe(val => { if (val) { doStuff(); } setTimeout(() => doOtherStuff(), 1000); }); I am wondering if there is another way to achieve this without the use of setTimeout, I was thinking about timer from rxjs, but I am not sure how that could be incorporated here. 回答1: You can use tap and delay : this.someFormGroup.controls['control1'].valueChanges .pipe( tap(val => { if (val) { doStuff(); } }), delay(1000), )

timeout inside a subscription rxjs

不羁岁月 提交于 2020-05-30 08:46:07
问题 I have the following code this.someFormGroup.controls['control1'].valueChanges.subscribe(val => { if (val) { doStuff(); } setTimeout(() => doOtherStuff(), 1000); }); I am wondering if there is another way to achieve this without the use of setTimeout, I was thinking about timer from rxjs, but I am not sure how that could be incorporated here. 回答1: You can use tap and delay : this.someFormGroup.controls['control1'].valueChanges .pipe( tap(val => { if (val) { doStuff(); } }), delay(1000), )