Angular $http POST 更改日期格式
Angular $http POST changes date format
以下情况:
我有一个带有 input[date]
字段的表单。我通过以下代码转换值:
$scope.entity.date = $filter('date')($scope.entity.date, 'yyyy-MM-dd');
这正确地将日期格式化为例如2015-10-27
当我使用 $http.post
提交实体时,angular 似乎将其识别为日期并将其重新格式化为 2015-09-30T23:00:00.000Z
。我在德国,我们有 GMT+1。所以 angular 将日期转换为 GMT。
有什么方法可以禁用此行为吗?
编辑:
HTML-代码:
<form ng-submit="submit()">
<input type="date" ng-model="entity.date" />
</form>
JS 代码:
$scope.submit = function() {
$scope.entity.date = $filter('date')($scope.entity.date, 'yyyy-MM-dd');
// when debugging this is the point where $scope.entity.date is 2015-10-27
// so it is the format and date I expect
$http({
data: $scope.entity,
method: POST,
url: '/resource'
})
.success(function(data) {
// do some stuff
});
// when looking into network traffic the request was sent with
// 2015-09-30T23:00:00.000Z as value for $scope.entity.date
};
您正在 post 上更改模型值。由于您的输入类型是日期,因此它会变回。这不是一个好主意,因为实际的表单元素只会在您 post 之后更改值。
任何时候在保存之前需要操作对象时,创建对象的副本都是一个不错的主意。这样它就会按照你期望的方式运行。
var entity = angular.copy($scope.entity);
然后post本地副本就可以了
$http({
data: entity,
method: POST,
url: '/resource'
})
以下情况:
我有一个带有 input[date]
字段的表单。我通过以下代码转换值:
$scope.entity.date = $filter('date')($scope.entity.date, 'yyyy-MM-dd');
这正确地将日期格式化为例如2015-10-27
当我使用 $http.post
提交实体时,angular 似乎将其识别为日期并将其重新格式化为 2015-09-30T23:00:00.000Z
。我在德国,我们有 GMT+1。所以 angular 将日期转换为 GMT。
有什么方法可以禁用此行为吗?
编辑:
HTML-代码:
<form ng-submit="submit()">
<input type="date" ng-model="entity.date" />
</form>
JS 代码:
$scope.submit = function() {
$scope.entity.date = $filter('date')($scope.entity.date, 'yyyy-MM-dd');
// when debugging this is the point where $scope.entity.date is 2015-10-27
// so it is the format and date I expect
$http({
data: $scope.entity,
method: POST,
url: '/resource'
})
.success(function(data) {
// do some stuff
});
// when looking into network traffic the request was sent with
// 2015-09-30T23:00:00.000Z as value for $scope.entity.date
};
您正在 post 上更改模型值。由于您的输入类型是日期,因此它会变回。这不是一个好主意,因为实际的表单元素只会在您 post 之后更改值。
任何时候在保存之前需要操作对象时,创建对象的副本都是一个不错的主意。这样它就会按照你期望的方式运行。
var entity = angular.copy($scope.entity);
然后post本地副本就可以了
$http({
data: entity,
method: POST,
url: '/resource'
})