简体   繁体   中英

How can I call a function after all ajax calls have loaded?

I am using AngularJS. In a controller I called many ajax calls. After all data fetched from DB using ajax calls, I want to call a function. How can I do that?

If you are using $http to make your ajax calls, you can retrieve promises from those calls, and execute something when all calls ends with $q.all :

var promises = [];
promises.push($http.get('1'));
promises.push($http.get('2'));
promises.push($http.get('3'));

$q.all(promises).then(function (datas) {
    // all 3 ajax calls have returns
    // you can even retrieve datas from each ajax calls
    var data1 = datas[0];
    var data2 = datas[1];
    var data3 = datas[2];
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM