I have a factory in AngularJS set up like this:
'use strict';
angular.module('frontRplApp')
.factory('paymentService', function ($rootScope, $http, config, tools) {
var urlBase = config.baseUrl;
var paymentService = {
response: {},
create: function () {
var args = {};
return $http.post(urlBase + 'api/investor/payment/create', args);
}
});
I want to utilize this factory inside a controller as shown below. The key point here is handling different scenarios based on whether the request was successful or encountered an error.
$scope.order = function () {
console.log('PaymentCashCtrl.order');
$scope.disabledButtons.submitCashOrder = true;
paymentService.create()
.then(
function (response) {
// do something with response
}, function (error) {
// do something with an error
}));
};
My challenge is that I wish to update some fields within the paymentService once the $http.post response is resolved and then return the promise so that the callbacks in the controller, namely function(response) and function(error), continue to work.
I attempted the following approach:
return $http.post(urlBase + 'api/investor/payment/create', args)
.then(function(response){
console.log(response);
this.response = response;
return response;
});
However, this method does not work as the function(error) handler in the controller is never triggered.
I aim to keep using my controller handlers while also updating certain aspects when the $http.post response is resolved.
Thank you.