I am currently working on an Angular project that involves a controller and service.
In this setup, the controller sends data and an endpoint to the service.
As of now, the service handles the http request. However, I am in the process of refactoring my code so that the service can match the endpoint from an array list.
This is how the Controller looks:
app.controller('MyController', function($scope, myService) {
$scope.buttonClick = function(endpoint) {
myService.postAction(endPoint, $scope.postData)
.success(function (data, status, headers, config) {
console.log("success");
})
.error(function (data, status, headers, config) {
console.log("error");
});
}
And here's the MyService code:
app.factory('MyService', function ($http) {
var endPoints = [
'cart/cartDetails',
'customer/addressInfo',
'payment/ShippingInfo',
]
return {
postAction: function(endPoint, postData) {
return $http({
method: 'POST',
url: endPoint,
data: postData,
headers: {'Content-Type': 'application/json'}
});
}
};
});
When a button is clicked using $scope.buttonClick
, one of the endpoints is passed like so:
<button ng-click="buttonClick('ShippingInfo')>Shipping</button>
<button ng-click="buttonClick('addressInfo')>Address</button>
<button ng-click="buttonClick('certDetails')>Cart</button>