user8226204
user8226204

Reputation:

Multiple ajax call in angular2

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 
});

Upvotes: 1

Views: 1256

Answers (1)

Vi Elite
Vi Elite

Reputation: 136

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 => {

});

Upvotes: 1

Related Questions