Angular js - detect when all $http() have finished

后端 未结 3 1433
一生所求
一生所求 2020-12-12 19:47

Ok i have tons of $http() calls all around the app code,

i\'m wondering is there any way / best practice to detect when all $http() around

3条回答
  •  心在旅途
    2020-12-12 20:07

    It is possible if you execute all requests by $q.all

    $scope.request1 = $http.get('request1URL', {cache: false});
    $scope.request2 = $http.get('request2URL', {'cache': false});
    $scope.loading = true;
    
    $q.all([$scope.request1, $scope.request2]).then(function(values) {
         $scope.loading = false;
         // Do whatever you want
         // values[0], values[1]
    });
    

    Using ng-if, you can show and hide loading gif.

    In case, if you use different $http call, then have a count or array in the $rootScope and update them whenever $http complete. Based on the count or array.length enable the loading gif.

提交回复
热议问题