如何根据AngularJS中两个$http调用的完成情况进行函数调用?

如何根据AngularJS中两个$http调用的完成情况进行函数调用?,angularjs,Angularjs,在控制器中,我有以下调用: optionService.init($scope); optionService.getSubjects1($scope); optionService.getSubjects2($scope); optionService.getAbc($scope); 以下是optionService的代码: angular.module('common') .factory('optionService', ['$http',function ($http)

在控制器中,我有以下调用:

optionService.init($scope);
optionService.getSubjects1($scope);
optionService.getSubjects2($scope);
optionService.getAbc($scope);
以下是optionService的代码:

angular.module('common')
    .factory('optionService',
    ['$http',function ($http) {
        var factory = {
            init: function ($scope) {
                $scope.option = {};
            },
            getSubjects1: function ($scope) {
                var url = '/api/Subject1/GetSelect';
                $scope.loading++;
                $http.get(url)
                    .success(function (data, status, headers, config) {
                        $scope.option.subjects1 = data;
                        $scope.loading--;
                    })
                    .error(function (data, status, headers, config) {
                        $scope.loading--;
                        alert("Error: No data returned from " + url);
                    });
            },
            getSubjects2: function ($scope) {
                var url = '/api/Subject2/GetSelect';
                $scope.loading++;
                $http.get(url)
                    .success(function (data, status, headers, config) {
                        $scope.option.subjects2 = data;
                        $scope.loading--;
                    })
                    .error(function (data, status, headers, config) {
                        $scope.loading--;
                        alert("Error: No data returned from " + url);
                    });
            },
        }
        return factory;

    }]);
有没有办法调用optionService.getAbc取决于getSubjects1和getSubjects2调用的完成情况?如果有什么不同,我也将很快使用Angular 1.2。

$q.all(Promissions)
允许您将多个Promissions合并到一个中:

optionService.getAbc = function($scope) {
    $q.all([
       optionService.getSubjects1($scope), 
       optionService.getSubjects2($scope)
     ])
     .then(function() {
       //...
     });
 }
编辑。是的,您需要从
getSubjects
函数返回承诺。要将代码更改保持在最低限度,可以执行以下操作:

optionService.getSubjects1($scope) {
  var url = '/api/Subject1/GetSelect';
  $scope.loading++;
  return $http.get(url)
    .then(function (data, status, headers, config) {
      $scope.option.subjects1 = data;
      $scope.loading--;
    }, function (data, status, headers, config) {
      $scope.loading--;
      alert("Error: No data returned from " + url);
    });
}

使用
$http.then()
您可以在
getAbc()

中创建一个可以与其他承诺组合的新承诺,您应该重写
getSubject1
getSubject2
以返回承诺。同时,将
$scope
传递给方法并从服务方法更新作用域不是一种好的方法。服务方法应该返回代码中可分配的数据。这使您的服务更具可用性和可读性

如果在以下行中更改
getSubject1
getSubject2

getSubjects1: function () {
                var url = '/api/Subject1/GetSelect';
                return $http.get(url);
}
在控制器中,您可以执行以下操作:

$q.all([
       optionService.getSubjects1(), 
       optionService.getSubjects2()
     ])
     .then(function([data1, data2]) {
       //use data1 to update scope for subject1
       // use data2 to update scope for subject2
       //call any service method.
 });

这看起来不错,但是我需要对getSubjects1和getSubjects2做任何事情。即使我不改变他们的承诺,他们是否真的还了承诺?