admin管理员组文章数量:1363338
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()
}]);
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 :
http://jsfiddle/Wr8mL/
(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()
}]);
Share
Improve this question
edited Jan 27, 2014 at 11:45
Sebastien
asked Jan 27, 2014 at 11:36
SebastienSebastien
6,66014 gold badges59 silver badges105 bronze badges
1 Answer
Reset to default 12Before 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 plete 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/ecma-262/5.1/#sec-15.9.1.15
本文标签: javascriptDate in angularjs are sent in UTC in resourcesave()Stack Overflow
版权声明:本文标题:javascript - Date in angularjs are sent in UTC in $resource.$save() - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743780555a2537747.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论