lmcphers
lmcphers

Reputation: 468

Binding a service response in Angular JS

I am trying to send the http response as a JSON body to an error handler if an error occurs. I am not really sure how to do this as I am a little inexperienced in this area. Here is the relevant code that I have currently:

Controller:

for (var prop in $scope.applicants) {
            var applicant = $scope.applicants[prop];
            $scope.sendApplicantsToSR(applicant).then(null, $scope.sendATSError.bind(null, applicant));
       }

$scope.sendATSError = function (applicant, error) {
        return AtsintegrationsService.applicantErrorHandling(applicant.dataset.atsApplicantID);
    };

    $scope.sendApplicantsToSR = function(applicant) {
        return AtsintegrationsService.sendApplicantsToSR(applicant);
    };

Service:

srvc.sendApplicantsToSR = function (applicant) {
    var applicantURL = {snip};

    return $http({
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        method: 'POST',
        url: applicantURL,
        data: applicant
    });
  };

  srvc.applicantErrorHandling = function (applicantID, error) {
    var url = srvc.url + {snip};

    return $http({
        method: 'POST',
        url: url,
        data: { "error_message": error }
    });
  };

So, ideally, I would like to pass the result of $scope.sendApplicantsToSR to $scope.sendATSError only when an error occurs.

Upvotes: 0

Views: 73

Answers (1)

Senanayaka
Senanayaka

Reputation: 309

Inside your controller

YourService.getdatafromservice().then(function(SDetails) {
  //response from service
  console.log(SDetails);
});

Inside your service

return {
  getData: getData
};

function getData() {
  var req = $http.post('get_school_data.php', {
    id: 'id_value',

  });

  return req.then(handleSuccess, handleError);

  function handleSuccess(response) {
    response_data = response.data;
    return response_data;
  }

  function handleError(response) {
    console.log("Request Err: ");
  }

}

Upvotes: 3

Related Questions