1

I want to write below jquery code in angular2. How can I do that?

$.when($.ajax("url"), $.ajax("/url2"))
  .then(myFunc, myFailure);

and

$.when( $.ajax("/req1"), $.ajax("/req2"), $.ajax("/req3") ).then(function(resp1, resp2, resp3){ 
    // plot graph using data from resp1, resp2 & resp3 
});
  • 1
    https://stackoverflow.com/questions/34405039/combining-promises-in-angular-2 – yurzui Jul 18 '17 at 05:25
  • Possible duplicate of [Combining promises in Angular 2](https://stackoverflow.com/questions/34405039/combining-promises-in-angular-2) – CozyAzure Jul 18 '17 at 05:27

1 Answers1

1

First you have learn how to use Angular Http module .

Then learn how to use RxJS to combine / join your http request altogether.

Reference for Http: https://angular.io/tutorial/toh-pt6 Reference for forkJoin : https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/forkjoin.md

let request$ = this.http.get('https://yourapi/api/').map(res => res.json());
let request2$ = this.http.get('http://another/api2').map(res => res.json());

Observable.forkJoin([request$, request2$]).subscribe(results => {

});
Vi Elite
  • 136
  • 2
  • 6
  • Thanks, Can you please tell, how to use the results data to different components abc.component.ts, bcd.component.ts(Here, first component requires response for first request and second component requires response for second request and so on) –  Jul 18 '17 at 05:43
  • Much depends on how you are going to structure your component, but I would think you will need to write a service for fetching http response , then inject a service for each of the component. But if 2 different components needs different data, I would not think how combining response is profitable here . Maybe you want all the data to be fetched before rendering any graphs ? – Vi Elite Jul 18 '17 at 06:30