Good afternoon, I decided to try rxjs, however, I don’t really understand how to remove the resulting data from the Observable:

import {Injectable} from "@angular/core"; import {Http, Response} from "@angular/http"; import {Observable} from "rxjs/Rx"; @Injectable() export class ServiceProvider { combined:any; constructor(private http:Http) { this.loadData(); } loadData() { Observable.forkJoin( this.http.get('data/data0.json').map((res:Response) => res.json()), this.http.get('data/data1.json').map((res:Response) => res.json()) ).subscribe(res => this.combined = res); console.log(this.combined); } } 

Here is a small class, as a result of the work should be an array with packed data from two different requests. However, ultimately comes undefined. I understand that it is necessary to snatch some event or something else. But I can't understand with my mind.

    1 answer 1

    Try this:

     import {Injectable} from "@angular/core"; import {Http, Response} from "@angular/http"; import {Observable} from "rxjs/Rx"; @Injectable() export class ServiceProvider { combined:any; constructor(private http:Http) { this.loadData(); } loadData() { let self = this; Observable.forkJoin( this.http.get('data/data0.json').map((res:Response) => res.json()), this.http.get('data/data1.json').map((res:Response) => res.json()) ).subscribe(res => { self.combined = res; console.log(self.combined); }); } } 
    • Yes, it works that way, but how, for example, make the return of this combined So that it turns out loadData () {... return combined} - slowkazak
    • @SoSlow Slightly changed the answer. - Alexandr
    • @SoSlow Normally, a service should return a ready Observable, and client classes call subscribe for this serviceAlexandr