I have an angular controller and service.
我有一个角度控制器和服务。
The controllers calls the service, and passes some data and an endpoint.
控制器调用服务,并传递一些数据和端点。
My service performs the http request, however i am trying to refactor my code so that i pass in an endpoint from my controller, and my service tries to match the endpoint from a list of array.
我的服务执行http请求,但是我试图重构我的代码,以便从控制器传入端点,我的服务尝试匹配数组列表中的端点。
Controller:
控制器:
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");
});
}
MyService:
为MyService:
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'}
});
}
};
});
Depending on what button is clicked via $scope.buttonClick
, any of the endpoints are passed, e.g.
根据通过$ scope.buttonClick单击的按钮,可以传递任何端点,例如
<button ng-click="buttonClick('ShippingInfo')>Shipping</button>
<button ng-click="buttonClick('addressInfo')>Address</button>
<button ng-click="buttonClick('certDetails')>Cart</button>
2 个解决方案
#1
2
Your endPoints should be object
你的endPoints应该是对象
app.factory('MyService', function ($http) {
var endPoints = {'certDetails': 'cart/cartDetails','addressInfo': 'customer/addressInfo','ShippingInfo': 'payment/ShippingInfo'}
return {
postAction: function(endPoint, postData) {
return $http({
method: 'POST',
url: endPoints[endPoint],
data: postData,
headers: {'Content-Type': 'application/json'}
});
}
};
});
#2
2
I suggest not to do that, cause controller in that case need to know at least key of the url endpoint It would be much better to have the following, your factory code:
我建议不要这样做,因为在这种情况下控制器至少需要知道url端点的关键字。如果有以下工厂代码会更好:
var endPoints = {'certDetails': 'cart/cartDetails',
'addressInfo': 'customer/addressInfo',
'shippingInfo': 'payment/ShippingInfo'}
return {
postCertDetails: post(endPoints['certDetails']),
postAddressInfo: post(endPoints['addressInfo']),
postShippingInfo: post(endPoints['shippingInfo'])
};
function post(endpoint){
return function(postData){
return $http({
method: 'POST',
url: endpoint,
data: postData,
headers: {'Content-Type': 'application/json'}
});
}
}
usage in controller
控制器中的用法
service.postCertDetails({...your data...});
#1
2
Your endPoints should be object
你的endPoints应该是对象
app.factory('MyService', function ($http) {
var endPoints = {'certDetails': 'cart/cartDetails','addressInfo': 'customer/addressInfo','ShippingInfo': 'payment/ShippingInfo'}
return {
postAction: function(endPoint, postData) {
return $http({
method: 'POST',
url: endPoints[endPoint],
data: postData,
headers: {'Content-Type': 'application/json'}
});
}
};
});
#2
2
I suggest not to do that, cause controller in that case need to know at least key of the url endpoint It would be much better to have the following, your factory code:
我建议不要这样做,因为在这种情况下控制器至少需要知道url端点的关键字。如果有以下工厂代码会更好:
var endPoints = {'certDetails': 'cart/cartDetails',
'addressInfo': 'customer/addressInfo',
'shippingInfo': 'payment/ShippingInfo'}
return {
postCertDetails: post(endPoints['certDetails']),
postAddressInfo: post(endPoints['addressInfo']),
postShippingInfo: post(endPoints['shippingInfo'])
};
function post(endpoint){
return function(postData){
return $http({
method: 'POST',
url: endpoint,
data: postData,
headers: {'Content-Type': 'application/json'}
});
}
}
usage in controller
控制器中的用法
service.postCertDetails({...your data...});