我有一个使用<input type="datetime-local" ng-bind="course.endDate"..
的表单并设置模型的变量。
在将日期发送到服务器之前,我要将日期2015-04-04T22:00:00.000Z
转换为integer
给出的getTime()
。
在控制器中我添加了这个:course.endDate = course.endDate.getTime();
它适用于服务器端,但在控制台中this error会出现角度抱怨。 (如上所述,它有效,但我想避免错误)
Error: [ngModel:datefmt] Expected `1325458800000` to be a date
http://errors.angularjs.org/1.3.15/ngModel/datefmt?p0=1325458800000
at REGEX_STRING_REGEXP (angular.js:63)
at Array.<anonymous> (angular.js:19938)
at Object.ngModelWatch (angular.js:23419)
at Scope.$get.Scope.$digest (angular.js:14300)
at Scope.$get.Scope.$apply (angular.js:14571)
at done (angular.js:9698)
at completeRequest (angular.js:9888)
at XMLHttpRequest.requestLoaded (angular.js:9829)
我该怎么办?
我有想法添加一些在表单中使用的字段(formEndDate
)并转换为服务器端的另一个字段(endDate = formEndDate.getTime()
),但这样服务器拒绝调用因为不允许参数formEndDate
,如果我删除了formEndDate
,那么一切都会中断。
其他问题: 当我从服务器获取数据时,我有一个整数,需要转换为在表单中使用的日期。所以我还要在允许编辑之前转换日期。我怎样才能做到这一点? (获取的数据是一个数组,因此无需迭代整个数组就可以进行转换)
感谢两个答案(我设置了正确的第一个)我也(不知何故)在编辑时解决了表单的问题。我这样做是通过创建一个额外的字段并在编辑时将其用于表单(我进行内联编辑)。
我创建了一个要点here
答案 0 :(得分:2)
在将数据发送到服务器之前,请复制并设置var Users = new openerp.website.Model('res.users');
。然后将副本发送到服务器:
endDate
答案 1 :(得分:2)
在角度中,将所有表单数据保存在一个属性下是很好的,例如:
$scope.formData = {endDate : 'xxx', ...};
在将数据发送到服务器之前,您需要创建formData
:
var formDataCopy= angular.copy($scope.formData);
然后,您可以对给定的副本进行任何转换操作。任何变更都不会影响范围内的数据。
答案 2 :(得分:0)
你可以使用transformRequest
转换请求的正文,就像这样(取自official docs:
function appendTransform(defaults, transform) {
// We can't guarantee that the default transformation is an array
defaults = angular.isArray(defaults) ? defaults : [defaults];
// Append the new transformation to the defaults
return defaults.concat(transform);
}
$http({
url: '...',
method: 'GET',
transformRequest: appendTransform($http.defaults.transformRequest, function(value) {
// transform the payload here
return value;
}),
transformResponse: appendTransform($http.defaults.transformResponse, function(value) {
// transform the response here
return value;
})
});