observable

Angular 5 synchronous HTTP call

旧巷老猫 提交于 2020-02-01 00:35:05
问题 I have an Angular 5 application in which I have to call some heavy REST service (usually takes some seconds). I need its result in different part of application, so I would like to store the result in a DataStorageService. Basically, this is I would like to achieve: @Injectable() export class DataStorageService { private result: MyCustomObject; constructor(private service: Service) {} getResult(): MyCustomObject { if (typeof this.result === 'undefined') { // save result } return result; } The

Angular 5 synchronous HTTP call

梦想与她 提交于 2020-02-01 00:34:54
问题 I have an Angular 5 application in which I have to call some heavy REST service (usually takes some seconds). I need its result in different part of application, so I would like to store the result in a DataStorageService. Basically, this is I would like to achieve: @Injectable() export class DataStorageService { private result: MyCustomObject; constructor(private service: Service) {} getResult(): MyCustomObject { if (typeof this.result === 'undefined') { // save result } return result; } The

Angular 8 How to get value from observable in ngOnInit and how do they behave [duplicate]

倾然丶 夕夏残阳落幕 提交于 2020-01-25 10:13:40
问题 This question already has answers here : Angular - two subscriptions in ngOnInit result in object 'undefined' (1 answer) Observable Undefined (1 answer) Closed 15 days ago . Hello Community , i am trying to use the HttpClient Module to get some data from my api. The api calls are defined in my service. I call them in Lifecyclehooks in my component but i struggle to understand the behavior of the returned observables and how i get the value of it in ngOnInit. I built a sample code which logs

Waiting for observable to finish - Angular

送分小仙女□ 提交于 2020-01-25 08:28:05
问题 I'm using Angular and Firebase storage to upload images and I'm having the problem where my urlImg it's undefined when I try to print it in the console.log inside of the tap . this.snapshot = this.snapshot = this.task.snapshotChanges().pipe( finalize(() => { this.storage .ref(path) .getDownloadURL() .subscribe(url => { this.urlImg = url; // with this you can use it in the html }); }), tap(snap => { if (snap.bytesTransferred === snap.totalBytes) { // Update firestore on completion //Code to

Handle multiple synchronous independent http request Angular 2

徘徊边缘 提交于 2020-01-23 16:48:56
问题 I'm trying to build an angular 2 application which shows multiple charts to users and i'm facing a real problem with handling multiple parallel independent http request , i got 505 response when i call more then 2 requests in ngOnInit. this._service.getPlant().subscribe(plants => { for (var i = 0; i < plants.length; i++) for (var name in plants[i]) { this.plants.push(plants[i][name]); console.log(plants[i][name]); } this._service.getDept().subscribe(depts => { for (var i = 0; i < depts.length

Angular observables - Do I need unsubscribe if no subscription?

送分小仙女□ 提交于 2020-01-23 11:42:16
问题 I am using latest angular 8 and am new to the concept of observables. Question I have if I am directly calling an observable and not apply it to a subscription variable, do I still need to unsubscribe. Below are the scenarios I would like to know if I need to unsubscribe on? Many thanks in advance Scenario 1 - Calling a httpService from a component: Service - httpService getContactsHttp(){ let headers: any = new HttpHeaders(this.authService.getHeadersClient()); return this.httpClient.get('

Rxjs Observable run sequentially and return an array

不问归期 提交于 2020-01-23 05:47:08
问题 var arr = [obs1, obs2, obs3]; Observable.forkJoin(...arr).subscribe(function (observableItems) {}) Runs observables in parallel and return an array. How can I run the observables sequentially and return an array. I do not want to get called for each observable, so concat() is not appropriate for me. I want to receive the same final result as forkJoin but run sequentially. Does it exist? or do I have to code my own observable pattern? 回答1: Just use concat and then toArray : var arr = [obs1,

Angular RxJS Observable: takeUntil vs. unsubscribe with a Subscription [closed]

心不动则不痛 提交于 2020-01-21 06:07:47
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 3 months ago . There are several ways to unsubscribe from observables on Angular components (by using ngOnDestroy). Which option below should be preferred and why (e.g. technical reasons, performance, etc.)? Option 1: takeUntil Using RxJS takeUntil to unsubscribe @Component({ selector:

How can I create an observable with a delay

99封情书 提交于 2020-01-19 06:51:07
问题 Question For testing purposes, I'm creating Observable objects that replace the observable that would be returned by an actual http call with Http . My observable is created with the following code: fakeObservable = Observable.create(obs => { obs.next([1, 2, 3]); obs.complete(); }); The thing is, this observable emits immediatly. Is there a way to add a custom delay to its emission? Track I tried this: fakeObservable = Observable.create(obs => { setTimeout(() => { obs.next([1, 2, 3]); obs

Evaluate subscription callback at time observable is created

孤街醉人 提交于 2020-01-17 15:49:08
问题 I have a Reactive Form in which it appears the function inside the following subscription evaluates itm["value"].length-1 at the time the observable emits data (and calls the subscription function). this.formCtls[controlName] = new FormControl('', {updateOn: 'blur'}); this.userForm.addControl(controlName, this.formCtls[controlName]); this.formCtls[controlName].valueChanges.subscribe(val=>{ itm["value"][itm["value"].length-1]=val; this.renderDataArray(); }); However I want the subscription