rxjs

RXJS Wait for All Observables to Complete and Return Results

一世执手 提交于 2019-12-10 16:59:51
问题 I'm trying to create a RX stream that will execute a list of XHR calls async and then wait for them to complete before going to the next call. To help explain this could be written like this in normal JS: try { await* [ ...requests.map(r => angularHttpService.get(`/foo/bar/${r}`)) ]; } catch(e) { throw e } // do something This is the code I was trying but its running them individually and not waiting for them all to complete before proceeding. (This is a NGRX Effect stream so it is slightly

Subscribing to an observable in an Angular 2 HTML element

半世苍凉 提交于 2019-12-10 16:46:39
问题 I have an observable producing Users that have an isLoading property, such that this yields the expected result: {{ (user$ | async).isLoading }} I would like to be able to use this isLoading property in an HTML attribute, like this: <button md-raised-button type="submit" [disabled]="user$.isLoading">Login</button> but no syntax I use seems to do the trick. How do you subscribe to an observable like this in an HTML attribute? 回答1: You can use async pipe in html tag like in example below, just

How to transform a Subject into an Observable in RxJs 5

冷暖自知 提交于 2019-12-10 16:38:22
问题 How can we transform a Subject into an Observable in RxJs 5 ? This functionality is useful for example when we want to expose the Subject for subscription but don't want to yield control of calling next() on it, and prefer to keep the issuing of new values private. The docs (see here) mention something like this: var subject = new Rx.Subject(); var obs = subject.asObservable(); But in RxJs 5 this currently does not work (alpha 8), we get the following error: "TypeError: subject.asObservable

Why use .takeUntil() over .take(1) with Http service?

依然范特西╮ 提交于 2019-12-10 16:38:07
问题 Context: I am working on implementing @ngrx/effects within an @ngrx/store project and studying the example app. Question: In the BookEffects class file, line #50, why is takeUntil(...) used instead of take(1) ? Both would seem to accomplish the same thing in this case. @Injectable() export class BookEffects { constructor(private actions$: Actions, private googleBooks: GoogleBooksService) { } @Effect() search$: Observable<Action> = this.actions$ .ofType(book.ActionTypes.SEARCH) .debounceTime

Is it bad practice to use many nested switchMap?

自闭症网瘾萝莉.ら 提交于 2019-12-10 16:17:37
问题 I have http interceptor. In that interceptor, before I change the request, I need a loader to turn on. What really worries me is that I end up having lots of switchmaps. why? loader is asynchronous I also need to translate the message passing from interceptor to loader service. translating message is also asyncrhonous. and in interceptor, i should run the request when loader and translating finishes What I do in my loader service showLoader(message){ return this.translateService.get(message)

How to use custom pipe on async pipe?

*爱你&永不变心* 提交于 2019-12-10 15:54:36
问题 I'm trying to create custom pipe on asynchronous pipe, I tried many solutions, but still not working. Here is the snippet of code. product.sort.ts - custom pipe import { PipeTransform, Pipe } from '@angular/core'; import { Observable } from 'rxjs/Observable'; @Pipe({ name: 'sortByName' }) export class ProductPipe implements PipeTransform{ /*transform(values: Array<any>, term:string){ return values.filter(obj => obj.pname.startsWith(term)) }*/ //CODE NOT WORKING >>>>> transform($value:

sane way to pass/keep a value throwout a long pipe

安稳与你 提交于 2019-12-10 15:43:16
问题 Assuming i have the following rxjs pipe: start$.pip( map((id)=> {}), //I want to save the "id" value to be used in the end of the pipe map(...), switchMap(...), map(...), switchMap(...), map(...), switchMap(...), switchMap(...)//I need the original "id" value here ).subscribe() Is there a way to keep the 'id" value throughout the pip so it can be used at the end of the pipe? Motivation: It comes often in NGRX effects where i want to use the original payload data of the triggering source

RxJS Map array to observable and back to plain object in array

≯℡__Kan透↙ 提交于 2019-12-10 15:38:37
问题 I have an array of objects from which I need to pass each object separately into async method (process behind is handled with Promise and then converted back to Observable via Observable.fromPromise(...) - this way is needed because the same method is used in case just single object is passed anytime; the process is saving objects into database). For example, this is an array of objects: [ { "name": "John", ... }, { "name": "Anna", ... }, { "name": "Joe",, ... }, { "name": "Alexandra", ... },

How to check if a RxJS Observable contains a string in Angular2?

 ̄綄美尐妖づ 提交于 2019-12-10 15:34:13
问题 I am new to Angular2 and Observable, I want to check if a Observable getRoles which is of type Observable<string[]> contains a string. public hasRole(name: string): boolean { // getRoles is of type Observable<string[]> let getRoles = this.tokenService.getTokenInformation().map(element => element.roles); if (/* check if name is inside of getRoles */) { return true; } return false; } 回答1: Observables are asynchronous so you can't use let getRoles = ...map(...) . The map() method is not executed

Angular 6 http.delete request not working

旧城冷巷雨未停 提交于 2019-12-10 15:29:18
问题 I cant seem to get my delete request to work. I have finished all of the get requests but now I'm stuck on delete and can't seem to wrap my head around it. The console.log'd URL is always correct and the delete request works fine via Postman. Got any ideas? HTML <button class="button button3" (click)="delTicket()"><span class="fa fa-trash"></span></button> TS delTicket(){ this.id = this.route.snapshot.params.id; this.ticketService.deleteTicket(this.id); } Service deleteTicket(id): Observable