angular2中的多个ajax调用 [英] Multiple ajax call in angular2

查看:73
本文介绍了angular2中的多个ajax调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在angular2中编写下面的jQuery代码.我该怎么办?

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

推荐答案

首先,您学习了如何使用Angular Http模块.

First you have learn how to use Angular Http module .

然后学习如何使用RxJS完全组合/加入您的http请求.

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

对Http的引用: https://angular.io/tutorial/toh-pt6 forkJoin的参考: https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/forkjoin.md

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

});

这篇关于angular2中的多个ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆