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

1 Answer 1

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

});
Sign up to request clarification or add additional context in comments.

2 Comments

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)
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 ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.