Skip to content

angular请求多个接口之后再统一返回数据 #18

@Narutocc

Description

@Narutocc

angular请求多个接口之后再统一返回数据

promise()方法

循环组成数组,拼接

var queryApi = [];
angular.forEach(res,function(data,index,array){
	var ajaxApi = $.ajax("...");
	queryApi.push(ajaxApi)
})
Promise.all(queryApi).then(function(res){
	...
})

异步请求多个接口:

第一种:promise()

Promise.all([p1, p2, p3]).then(function (results) { 
    console.log(results);  // [1, 2, 3] 
});

第二种:when()

$.when([$.ajax('/request1'), $.ajax('/request2'), $.ajax('/request3')]).done(function (data) { 
     console.log('第一个ajax请求',data[0]); 
     console.log('第二个ajax请求',data[1]); 
     console.log('第三个ajax请求',data[2]); 
});

之后触发数据更新 $apply()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions