Ben*_*nny 5 angularjs angularjs-filter
以下情况:
我有一个带有input[date]字段的表单。我通过以下代码转换值:
$scope.entity.date = $filter('date')($scope.entity.date, 'yyyy-MM-dd');
Run Code Online (Sandbox Code Playgroud)
这正确地将日期格式化为例如 2015-10-27
当我使用$http.postangular提交实体时,似乎将其识别为日期并将其重新格式化为2015-09-30T23:00:00.000Z. 我在德国,我们有 GMT+1。所以 angular 将日期转换为 GMT。有什么办法可以禁用这种行为吗?
编辑:
HTML代码:
<form ng-submit="submit()">
<input type="date" ng-model="entity.date" />
</form>
Run Code Online (Sandbox Code Playgroud)
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
};
Run Code Online (Sandbox Code Playgroud)
您正在更改发布时的模型值。由于您的输入类型是日期,因此它会变回来。这不是一个好主意,因为实际的表单元素只有在您发布后才会更改值。
每当您需要在保存之前操作对象时,创建对象的副本都是一个不错的主意。这样它就会按照您期望的方式运行。
var entity = angular.copy($scope.entity);
Run Code Online (Sandbox Code Playgroud)
然后发布本地副本就可以了。
$http({
data: entity,
method: POST,
url: '/resource'
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3976 次 |
| 最近记录: |