rxjs

Make parallel calls http get or post calls in angular 2

余生颓废 提交于 2020-04-13 06:07:28
问题 How to make parallel calls HTTP get or post calls in angular 2? I have 2 service calls on the response of one callus have to make another call. Could some suggest me how to call make these parallel calls with error handling scenarios? 回答1: If your service is Observable based instead of Promise you can do forkJoin . It runs all observable sequences in parallel. For RxJS version < 6 import 'rxjs/add/observable/forkJoin'; Make sure to import forkJoin from rxjs library Observable.forkJoin

How to use BehaviourSubjects to share data from API call between components in Angular?

蓝咒 提交于 2020-04-07 08:09:06
问题 I am currently building an Angular application where I make a request to an api, and I map the repsonse to two different arrays. I can use this data in my app.components.ts but I will make new components for what I need. How can I share the data between components to ensure that the components always have the latest data because I will also need to periodically call the API. I've seen some answers on SO and some youtube videos but I'm just not fully understanding it. The code of my service is

Angular RxJs - Getting subscriber instead of the required value

天大地大妈咪最大 提交于 2020-03-26 01:39:11
问题 Prior to getting the current customer orders I need to return his name, so my related Service class part looks like the following: Updated: export class AuthService { customerNameUrl = 'customers/name/' + name; . . . getCustomerOrders(customerName: string): Observable<CustomerOrder[]> { let currentCustomerName = this.getCurrentCustomer(customerName).subscribe(customer => customer.name); console.log(currentCustomerName); <--- Error let customerOrders = this.http.get<CustomerOrder[]>(this

Angular 'values is undefined' subscribing to mapped http response (request not being made?)

不问归期 提交于 2020-03-23 12:01:33
问题 I have a simple login form component ( LoginComponent ) that calls the submitLogin method. import { Component, OnInit } from '@angular/core'; import { FormControl } from '@angular/forms'; import { Router, ActivatedRoute } from '@angular/router'; import { first } from 'rxjs/operators'; import { AuthenticationService } from '../../services'; @Component({ selector: 'login', templateUrl: './login.component.html', styleUrls: ['./login.component.scss'] }) export class LoginComponent implements

Get observable return value without subscribing in calling class

妖精的绣舞 提交于 2020-03-23 09:02:42
问题 In TypeScript / Angular, you would usually call a function that returns an observable and subscribe to it in a component like this: this.productsService.getProduct().subscribe((product) => { this.product = product }); This is fine when the code runs in a class that manages data, but in my opinion this should not be handled in the component. I may be wrong but i think the job of a component should be to ask for and display data without handling how the it is retrieved. In the angular template

Are http operations of the httpClient always Single value Observables? Can they somehow emit more then one value?

五迷三道 提交于 2020-03-23 02:04:48
问题 I have a hard time to grasp the way http Observables work. Http get always completes, when only one value arrives but there is no way to look up the implementation. Do they always complete after error or a value arrives? I have a lot of discussion with my collegues, because the use in every http operation the following procedure: const sub = this.http.get( enviroment.baseUrl + '/users').pipe(take(1)) .subscribe( value => { //do something with the value }, error => consol.log(error.message));

What are the differences between using a Subject and an Observable, and what are the uses for each?

扶醉桌前 提交于 2020-03-22 03:40:26
问题 I have learned of two different ways of making an Observable. First one was with a Subject, like so: // file A const message$ = new Subject(); // file B message$.subscribe( (message) => console.log(message) ); // file C message$.next("Hello there!"); This method of creating an Observable allows me to have a way to exchange data from file B to file C. The second way is via the Observable class, like so: // file A const click$ = new Observable( function(observer) { //Alternatively, I can use

RxJs/NgRx - is there a way to “Cancel” a stream after the delay operator

烂漫一生 提交于 2020-03-20 19:41:32
问题 I am using a polling scheme in my Angular application using NgRx. To simplify things, I have something like the following... public stopPolling$ = createEffect(() => this.actions$.pipe( ofType(actions.stopPolling), tap(_ => this.isPollingActive = false), map(_ => actions.stopPolling()) ), { dispatch: false }); public continuePolling$ = createEffect(() => this.actions$.pipe( ofType(actions.getData), tap(_ => this.logger.debug('continue polling')), delay(8000), switchMap(_ => this.pollData()) )

RxJs/NgRx - is there a way to “Cancel” a stream after the delay operator

别来无恙 提交于 2020-03-20 19:39:32
问题 I am using a polling scheme in my Angular application using NgRx. To simplify things, I have something like the following... public stopPolling$ = createEffect(() => this.actions$.pipe( ofType(actions.stopPolling), tap(_ => this.isPollingActive = false), map(_ => actions.stopPolling()) ), { dispatch: false }); public continuePolling$ = createEffect(() => this.actions$.pipe( ofType(actions.getData), tap(_ => this.logger.debug('continue polling')), delay(8000), switchMap(_ => this.pollData()) )

RxJs/NgRx - is there a way to “Cancel” a stream after the delay operator

馋奶兔 提交于 2020-03-20 19:39:25
问题 I am using a polling scheme in my Angular application using NgRx. To simplify things, I have something like the following... public stopPolling$ = createEffect(() => this.actions$.pipe( ofType(actions.stopPolling), tap(_ => this.isPollingActive = false), map(_ => actions.stopPolling()) ), { dispatch: false }); public continuePolling$ = createEffect(() => this.actions$.pipe( ofType(actions.getData), tap(_ => this.logger.debug('continue polling')), delay(8000), switchMap(_ => this.pollData()) )