Reputation: 145
I have two requests with the same object result ("Data"). I would get the two responses and put the all in the array ("Data[]"). example :
In my service:
getData1(): Observable<Data> {
return this.http
.get<Data>(`${url}/data`):
}
getData2(): Observable<Data> {
return this.http
.get<Data>(`${url}/data`):
}
How I can make for get the result of the two methods and put the all in the data[] for my component?
UPDATE SOLUTION
service
getData1(): Observable<Data> {
return this.http
.get<Data>(`${url}/data`):
}
getData2(): Observable<Data> {
return this.http
.get<Data>(`${url}/data`):
}
getDatas(): Observable<GetData[]> {
return forkJoin([this.getData1(), this.getData2()]);
}
component
getDatas: GetData[];
ngOnInit() {
this.dataService.getDatas().subscribe(x => this.getDatas= x);
}
I can make a loop on *ngFor="let data of datas"
It's work
Upvotes: 2
Views: 1539
Reputation: 17494
Use ForkJoin
as below:
getData(): Observable<Data[]> {
const req = [];
req.push(this.http.get<Data>(url_1));
req.push(this.http.get<Data>(url_2));
return forkJoin(req);
}
This will return response as an array
where the response of both url will be provided
Upvotes: 2