对 Angular.js 中 $http 请求的结果进行排序
Put an order to the result of $http requests in Angular.js
我有 3 个通过 $http 执行的 Web 请求,此请愿书在函数中 (function1()
, function2()
, function3()
) 。我想自定义所有这些请求的执行顺序。
object.function1().then(function() {
//result of petition $http of function1();
});
object.function2().then(function() {
//result of petition $http of function1();
});
object.function3().then(function() {
//result of petition $http of function2();
});
他们都试图同时 运行。有些请求比其他请求花费的时间更长,因为它们获得了更多 JSON 个对象。我想 运行 以便开始:
function1(); //first
function2(); //second
function3(); //three
您需要正确使用.then()
方法:
object.function1().then(function(result) {
//result of petition $http of function1();
return object.function2()
}).then(function (result) {
//result of petition $http of function2();
return object.function3();
}).then(function (result) {
//result of petition $http of function3();
});
像这样在其他函数的回调中调用函数:
object.function1().then(function() {
//result of petition $http of function1();
object.function2().then(function() {
//result of petition $http of function1();
object.function3().then(function() {
//result of petition $http of function2();
});
});
});
你可以试试这个。
$http.get('URL1').success(function(data){
$http.get('URL2').success(function(data){
$http.get('URL3').success(function(data){
console.log('Done');
}
}
});
我有 3 个通过 $http 执行的 Web 请求,此请愿书在函数中 (function1()
, function2()
, function3()
) 。我想自定义所有这些请求的执行顺序。
object.function1().then(function() {
//result of petition $http of function1();
});
object.function2().then(function() {
//result of petition $http of function1();
});
object.function3().then(function() {
//result of petition $http of function2();
});
他们都试图同时 运行。有些请求比其他请求花费的时间更长,因为它们获得了更多 JSON 个对象。我想 运行 以便开始:
function1(); //first
function2(); //second
function3(); //three
您需要正确使用.then()
方法:
object.function1().then(function(result) {
//result of petition $http of function1();
return object.function2()
}).then(function (result) {
//result of petition $http of function2();
return object.function3();
}).then(function (result) {
//result of petition $http of function3();
});
像这样在其他函数的回调中调用函数:
object.function1().then(function() {
//result of petition $http of function1();
object.function2().then(function() {
//result of petition $http of function1();
object.function3().then(function() {
//result of petition $http of function2();
});
});
});
你可以试试这个。
$http.get('URL1').success(function(data){
$http.get('URL2').success(function(data){
$http.get('URL3').success(function(data){
console.log('Done');
}
}
});