2

Пример одного из моих $http запросов в AngularJS (структура других аналогична)

$scope.getCurrentCareers=function () {
        var promise = $http({
            url: basePath + "/studentreg/getcurrentcareers",
            method: "POST",
            headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'}
        }).then(function successCallback(response) {
            $scope.currentCareers=response.data;
        }, function errorCallback() {
            console.log("error");
        });
        return promise;
    };

В контроллере у меня нужно вызвать три разных сервиса, чтобы они выполнялись параллельно, а после того как последних из них(по времени) получит ответ - вызвать 4-ю функцию.

angular.element(document).ready(function () {
        $scope.getCurrentCareers();
        $scope.getCurrentSpecializations();
        $scope.getCurrentCountryCity();
// после последнего ответа вызвать функцию $scope.myFunction();
          .then(
             function () {$scope.myFunction();
          )};
)};

Подскажите как это сделать правильно?

1 ответ 1

2

Использовать либо Promise.all() из стандарта ECMAScript. Либо сервис ангуляра $q:

Пример нескольких запросов:

loadSomeInfo().then(function(something) {  
  loadAnotherInfo().then(function(another) {
    doSomethingOnThem(something, another);
  });
});

Переписать в такое:

$q.all([loadSomeInfo(), loadAnotherInfo()]).then(function (results) {
  doSomethingOnThem(results[0], results[1]);
});

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.