在angularjs日期在UTC在$资源)发送的。$保存( [英] Date in angularjs are sent in UTC in $resource.$save()
问题描述
我有angularjs的问题。我创建了一个工厂事件,当我使用$保存其实现方法具,d为日期是在发送和UTC不在浏览器时区...
我创建了一个的jsfiddle来说明吧:
(点击保存按钮,打开您的控制台,看看请求参数)
我的code:
window.App = angular.module(应用,[ngResource]);App.factory('事件',['$资源,$ HTTP',函数($资源,$ HTTP){
事件= $资源('/事件/:id.json',
{ID:@ ID},
{更新:{方法:把'},'查询':{方法:GET,IsArray的:假}});
返回事件;
}])App.controller(EventNewCtrl,['$范围,$ HTTP,事件功能($范围,$ HTTP,事件){ $ scope.resetEvent =功能(){
$ scope.event =新的事件({日期:新的Date()})
} $ scope.save =功能(){
。$ scope.event $保存();
} $ scope.resetEvent()}]);
中的数据将被提交之前,角变换你的对象与JSON.stringify功能。尝试
的console.log(JSON.stringify(新的Date()));
这是一样的:
的console.log(新的Date()toISOString());
(肯定第一个会被包装在引号)
有很多的可能性,以更改默认行为:
1)用自己的实现替换toISOString功能:
Date.prototype.toISOString =功能(){
回报在这里不用我的约会对象真棒格式化'+这一点;
};
2)换成你自己的角度默认的转变。您可以通过(通过 $ httpProvider.defaults.transformRequest
阉每个资源配置或为您完整的应用程序)提供transformRequest功能做到这一点。请参见 $ http服务了解详情。你的功能如下所示:
transformRequest:功能(数据){
返回您的字符串重数据presentation
}
如果你有兴趣,为什么toISODate剥离时区看看ECMAScript的语言特点规格:的 http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.1.15
I have an issue with angularjs. I created a factory Event and when I use the $save metho,d the date is send in UTC and not in the browser timezone...
I created a jsfiddle to illustrate it :
(Click on the save button and open your console to see request params)
My code :
window.App = angular.module('App', ["ngResource"]);
App.factory('Event', ['$resource', '$http', function($resource, $http) {
Event = $resource('/events/:id.json',
{ id:'@id' },
{ update: {method:'PUT' }, 'query': { method: 'GET', isArray: false }});
return Event;
}])
App.controller("EventNewCtrl", ['$scope', "$http", "Event", function($scope, $http, Event) {
$scope.resetEvent = function(){
$scope.event = new Event({ date: new Date() })
}
$scope.save = function() {
$scope.event.$save();
}
$scope.resetEvent()
}]);
Before the data will be submitted, angular transforms your object with the JSON.stringify function. Try a
console.log(JSON.stringify(new Date()));
this is the same as:
console.log(new Date().toISOString());
(for sure the first one will be wrapped in quotation marks)
There are a lot of possibilities to change the default behavior:
1) replace the toISOString function with your own implementation:
Date.prototype.toISOString = function(){
return 'here goes my awesome formatting of Date Objects '+ this;
};
2) replace the default transformation of angular with your own. You can do this by providing a transformRequest function (wether per resource configuration or for your complete app by $httpProvider.defaults.transformRequest
). See $http service for more information. Your function will look like this:
transformRequest: function(data){
return your string representation of the data
}
If you are interested why toISODate strips the timezone have a look at the ECMAScript Langauge Specification: http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.1.15
这篇关于在angularjs日期在UTC在$资源)发送的。$保存(的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!