angularjs $q、$http 处理多个异步请求
angularjs $q、$http 处理多个异步请求
在实际业务中经常需要等待几个请求完成后再进行下一步操作。但angularjs中$http不支持同步的请求。
解决方法一:
$http.get('url1').success(function (d1) {
$http.get('url2').success(function (d2) {
//处理逻辑
});
});
解决方法二:
then中的方法会按顺序执行。

var app = angular.module('app',[]);
app.controller('promiseControl',function($scope,$q,$http) {
function getJson(url){
var deferred = $q.defer();
$http.get(url)
.success(function(d){
d = parseInt(d);
console.log(d);
deferred.resolve(d);
});
return deferred.promise;
}
getJson('json1.txt').then(function(){
return getJson('json2.txt');
}).then(function(){
return getJson('json1.txt');
}).then(function(){
return getJson('json2.txt');
}).then(function(d){
console.log('end');
});
});

解决方法三:
$q.all方法第一个参数可以是数组(对象)。在第一参数中内容都执行完后就会执行then中方法。第一个参数的方法的所有返回值会以数组(对象)的形式传入。

var app = angular.module('app',[]);
app.controller('promiseControl',function($scope,$q,$http) {
$q.all({first: $http.get('json1.txt'),second: $http.get('json2.txt')}).then(function(arr){
console.log(arr);
angular.forEach(arr,function(d){
console.log(d);
console.log(d.data);
})
});
});

解决方法四:

var app = angular.module('app',[]);
app.controller('directiveControl',function($scope,$http,$q){
function getTxt(a) {
var deferred = $q.defer();
$http.get('1.json')
.success(function (d) {
console.log(a);
deferred.resolve();
})
return deferred.promise;
}
getTxt(1).then(function(){
return getTxt(2);
}).then(function(){
return getTxt(3);
}).then(function(){
return getTxt(4);
}).then(function(){
return getTxt(5);
}).then(function(){
console.log('end');
});
});

$q的详细使用方法网上的有很多教程。我也是刚接触。上面的代码是我按我的理解写的,经过了测试没有问题。
angularjs $q、$http 处理多个异步请求的更多相关文章
- ajax异步请求
做前端开发的朋友对于ajax异步更新一定印象深刻,作为刚入坑的小白,今天就和大家一起聊聊关于ajax异步请求的那点事.既然是ajax就少不了jQuery的知识,推荐大家访问www.w3school.c ...
- AJAX实现简单的注册页面异步请求
p { margin: 0px; padding: 0px } AJAX简介 (1)AJAX = 异步 JavaScript 和 XML. (2)AJAX 是一种用于创建快速动态网页的技术. (3)通 ...
- ASIHTTPRequest取消异步请求
今天碰到一个问题 异步请求等待中 cancel后会发生什么,网上找了下资料说取消的请求默认都会按请求失败处理,并调用请求失败delegate 查找到的资料具体解释了下ASIHTTPRequest取消异 ...
- Ajax_02之XHR发起异步请求
1.Ajax: AJAX:Asynchronous Javascript And Xml,异步的JS和XML: 同步请求:地址栏输入URL.链接跳转.表单提交-- 异步请求:使用Ajax发起,底层使用 ...
- JQuery中使用Ajax实现诸如登录名检测等异步请求Demo
上一篇博客介绍了注册登录时一次性图形验证码的工具类的编写,这篇随笔同样是我在写用jquery中ajax实现登录信息检测的异步请求功能的笔记,在各个网站进行信息用户注册时,需要在不刷新页面的情况下对注册 ...
- ASP.NET WebForm中异步请求防止XSRF攻击的方法
在ASP.NET MVC中微软已经提供了如何防止跨域攻击的方法.对于传统Webfrom中使用Handler来接受ajax的Post请求数据,如何来防止XSRF攻击呢.这里给大家提供一个简单地方法,和M ...
- 手动封装js原生XMLHttprequest异步请求
Code Object.extend =function(targetObj,fnJson){ //扩展方法,类似于jQuery的$.extend,可以扩展类的方法,也可以合并对象 for(var f ...
- jquery ajax异步请求
得先知道后台接口给ajax访问(接口URl和传入接口的参数及参数类型),知道访问之后返回的数据类型,有哪些数据. 选择异步请求的方式,常用的有三种,如$.ajax().$.post().$.get ...
- iOS NSURLConnection POST异步请求封装,支持转码GBK,HTTPS等
.h文件 #import <Foundation/Foundation.h> //成功的回调 typedef void(^successBlock)(id responseObj); // ...
- 使用Spring AsyncRestTemplate对象进行异步请求调用
直接上代码: package com.mlxs.common.server.asyncrest; import org.apache.log4j.Logger; import org.springfr ...
随机推荐
- VS2013无法链接到TFS(Visual studio online),错误TF31001,TF31002
TF31002: Unable to connect to VisualStudio Online with VS 2013 but I can using web access - Windows ...
- js单选和复选框
http://blog.csdn.net/chelen_jak/article/details/44827393 http://www.gbtags.com/technology/jquerynews ...
- C# Cut Line Bressenham Algorithm
using System; using System.Drawing; using System.Windows.Forms; namespace CutLine { static class Pro ...
- 东大OJ-Prim算法
1222: Sweep the snow 时间限制: 1 Sec 内存限制: 128 MB 提交: 28 解决: 18 [提交][状态][讨论版] 题目描述 After the big big s ...
- redis在window环境下的安装
1.下载客户端文件 地址:https://github.com/dmajkic/redis/downloads 客户端文件目录说明: 2.启动redis服务端 1.在客户端文件目录下新建一个bat文件 ...
- window配置nginx+php+mysql
博客来源: http://www.cnblogs.com/wuzhenbo/p/3493518.html 启用nginx D:\nginx>nginx.exe 重启 D:\nginx> ...
- openldap+phpadmin的搭建安装
1.概念介绍 LDAP是轻量目录访问协议,英文全称是Lightweight Directory Access Protocol,一般都简称为LDAP.它是基于X.500标准的,但是简单多了并且可以根据 ...
- 【BZOJ 4600】【SDOI 2016】硬币游戏
http://www.lydsy.com/JudgeOnline/problem.php?id=4600 转化成nim游戏 因为对于每一个反面朝上的硬币编号可以拆成\(2^a3^bc\),选择这个硬币 ...
- 【CodeVS 3160】最长公共子串
http://codevs.cn/problem/3160/ 看了好久的后缀自动机_(:з」∠)_ 对A串建立SAM,用B串去匹配A串SAM,如果在当前节点走不下去,就跳到当前节点的parent(类似 ...
- wordpress模板各文件函数解析
修改主题时发现好多WordPress主题函数都不了解,因此网上摘抄了一份放在自己博客上,便于以后好找. 在WordPress中如何按你的意愿显示页面,关键看你是否了解WordPress主题模板页面.这 ...