AngularJS 等待单个异步调用

AngularJS waiting for a single asynchronous call

我正在尝试将一些数据放在异步调用返回的范围内。
我在工厂里有一个名为 companies 的数组。

factory.getByCategoryId = function (id) {
  $http.get('http://localhost/campaign?access-token=12345&id=2').then(
    function (result) {
      factory.companies = [];
      if (result.data.length !== 0) {
        for (var index = 0; index < result.data.length; index++) {
          factory.companies.push(result.data[index]);
       }
      } else {
        console.log('Result is not set.', result);
      }
    },
    function (result) {
      console.log('Failed', result);
    }
  );
}

调用函数:

$scope.closeModal = function (index) {
  if (index) {
    var promises = []

    promises.push(Service.getByCategoryId($scope.categories[index].id));

    $q.all(promises).then(function () {
      $scope.myItems = Service.all(); //return all from factory.companies

      $scope.refreshItems(); //this function refreshes the DOM using myItems
    });
  }
}

第一次调用此函数factory.companies保持原样,但在第二次调用后更新。我确定该应用程序应该以某种方式等待更多但不知道如何等待。

    promises.push(Service.getByCategoryId($scope.categories[index].id));

所以你把 Service.getByCategoryIdpush 的 return 值取到 promises.

Service.getByCategoryId 的 return 值是多少?

它没有 return 语句,所以它是 undefined

您需要修改 Service.getByCategoryId 以便它 return 是一个承诺(大概是 $http.get 的 return 值)。

我只会 return 来自你们服务的 $http

在你的工厂:

factory.getByCategoryId = function (id) {
    return $http.get('http://localhost/campaign?access-token=12345&id=2');
}

还有你的函数

$scope.closeModal = function (index) {
    Service.getByCategoryId($scope.categories[index].id).then(
    function (result) {
        if (result.data.length) {
            angular.forEach(result.data, function(value) {
                $scope.myItems.push(value);
            });
            $scope.refreshItems();
        } else {
            console.log('Result is not set.', result);
        }
    },
    function (result) {
      console.log('Failed', result);
    }
};

或者您可以重写您的服务函数来解决承诺:

factory.getByCategoryId = function (id) {
    return $q(function (resolve, reject) {
        $http.get('http://localhost/campaign?access-token=12345&id=2').then(
            function (result) {
                factory.companies = [];
                if (result.data.length !== 0) {
                    for (var index = 0; index < result.data.length; index++) {
                        factory.companies.push(result.data[index]);
                    }
                    resolve(factory.companies);
                } else {
                    reject('Result is not set.', result);
                }
            },
            function (result) {
                console.log('Failed', result);
            }
        );
    });
}

并将其用作

$scope.closeModal = function (index) {
    Service.getByCategoryId($scope.categories[index].id).then(function(result) {
    $scope.myItems = result;
    $scope.refreshItems();
}, function(result) {
    console.log(result);
});

如果

Service.getByCategoryId($scope.categories[index].id)

然后返回一个承诺

$scope.closeModal = function (index) {
if (index) {
    Service.getByCategoryId($scope.categories[index].id).then(function (res) {
        $scope.myItems = Service.all(); //return all from factory.companies
        $scope.refreshItems(); //this function refreshes the DOM using myItems
    }, function (res) {
        // Error Handling
    })
 }
}

For Reference ::

app.factory('Service', function ($http) {
return {
    // Below function retuns a promsie
    getByCategoryId_promise: function () {
        // This simply sends the promise but you need to make it execute using $q 
        //or by entending like the closeModal method above
        return $http.get("URL GOES HERE");
    },
    // Below function retuns results from a promise
    getByCategoryId: function () {
        // This Function waits till the HTTP call is resolved and then sends the result
        return $http.get("URL GOES HERE").then(function (res) { }, function (res) { })
    },
}
})