我正在尝试整合我的所有服务,这些服务使得api调用Rails后端成为一个。这是它的样子:
angular.module('feeSuitesApp')
.factory('FeeSuitesSvc', [
'$http',
function ($http) {
var TransformInstance, Result;
TransformInstance = function(FeeObj, obj){
return new FeeObj(obj);
};
Result = function(feeSuiteObj){
var servicePlural = feeSuiteObj.service + 's';
return $http({
method: feeSuiteObj.method,
url: feeSuiteObj.url,
params: feeSuiteObj.params,
transformResponse: function(data){
var w = angular.fromJson(data);
angular.forEach(w[servicePlural], function(obj, idx){
w[servicePlural][idx] = TransformInstance(feeSuiteObj.feeObj, obj);
});
return w;
}
})
}
return {
promise: Result,
};
}]);
POST和GET工作正常,但PUT给了我一个RangeError: Maximum call stack size exceeded
FeeRule看起来像这样:
angular.module('feeSuitesApp')
.factory('FeeRule', function(){
//constructor
var FeeRule = function(obj){
this.id = obj.id || null;
this.name = obj.name || '';
this.fee_suite_id = obj.fee_suite_id || null;
this.fee_parameter_id = obj.fee_parameter_id || null;
this.multiplier = obj.multiplier || 1;
this.addend = obj.addend || 0;
};
return FeeRule;
});
然而,如果我进行以下调用,一切都运作良好:
var url = '/api/v3/fee_rules/' + feeRule.id;
$http.put(url, { fee_rule: feeRule});
但如果我像这样使用我的服务,我会收到堆栈溢出错误:
var feeSuiteObj = {
url: '/api/v3/fee_rules/' + feeRule.id,
service: 'fee_rule',
method: 'PUT',
params: {
fee_rule: feeRule
},
feeObj: FeeRule
};
FeeSuitesSvc.promise(feeSuiteObj).then(function(success){
$state.go('root');
},function(error){
console.log(error);
});
答案 0 :(得分:0)
请参阅https://docs.angularjs.org/api/ng/service/ $ http#usage,没有服务或 feeObj 配置属性,还使用 params 追加序列化数据到 GET 请求(不是 PUT )。所以试试这个:
var feeSuiteObj = {
url: '/api/v3/fee_rules/' + feeRule.id,
method: 'PUT',
data: {
fee_rule: feeRule
}
};
如果您需要传递服务或 feeObj ,您可以在数据对象中执行此操作:
data: {
fee_rule: feeRule,
service: 'fee_rule',
feeObj: FeeRule
}