简体   繁体   English

如何在AngularJS中处理REST Api中的日期字段?

[英]How to handle date fields from a REST Api in AngularJS?

I've got a REST API that returns JSON data with dates in a standard ISO-8601 format: yyyy-MM-ddTHH:mm:ss: 我有一个REST API,它返回标准ISO-8601格式的日期的JSON数据:yyyy-MM-ddTHH:mm:ss:

{
    id: 4
    version: 3
    code: "ADSFASDF"
    definition: "asdflkj"
    type: "CONTAINER"
    value: "1234"
    active: "false"
    formula: false
    validTo: "2014-12-31T05:00:00"
    validFrom: "2010-12-31T10:00:00"
}

My issue is that I'm not quite sure how to deal with this in AngularJS. 我的问题是我不太确定如何在AngularJS中处理这个问题。 I have a $resource which GETs, POSTs, etc my API endpoints, but when my data is returned, it is stored in my JS object as as String. 我有一个$resource GETs,POSTs等我的API端点,但是当我的数据被返回时,它被存储在我的JS对象中,就像String一样。 I figure that it would be easier to handle as a Date() or a Moment(). 我认为它更容易处理为Date()或Moment()。

In Java, I can use a JsonDeserializer to ensure that all JSON data is properly converted prior to being assigned to the model, but I don't know if there is a similar mechanism in AngularJS. 在Java中,我可以使用JsonDeserializer来确保在分配给模型之前正确转换所有JSON数据,但我不知道AngularJS中是否存在类似的机制。

I've searched around, but can't seem to find anything that implements a generalized solution. 我已经四处寻找,但似乎找不到任何实现通用解决方案的东西。 I realize that I can use a transformResponse function in my $resource , but that seems like a lot of repeated configuration for every data type that may contain a date. 我意识到我可以在我的$resource使用transformResponse函数,但对于可能包含日期的每种数据类型,这似乎都有很多重复配置。

This leads me to wonder if returning the date in an ISO-8601 format is the best way to proceed? 这让我想知道以ISO-8601格式返回日期是否是继续进行的最佳方式? If AngularJS doesn't support it out of the box, I presume that there must be an easier way to deal with dates. 如果AngularJS不支持开箱即用,我认为必须有一种更简单的方法来处理日期。 How does one deal with dates in AngularJS? 如何处理AngularJS中的日期? Should I just be treating them as text/string objects, and have the API return a pre-formatted version? 我应该将它们视为文本/字符串对象,并让API返回预先格式化的版本吗? If so, what is the most flexible format to use in an HTML5 date input box, etc? 如果是这样,在HTML5日期输入框等中使用最灵活的格式是什么?

JSON does not support dates so you will need to send out dates as strings or integers, and convert them to Date objects when suitable. JSON不支持日期,因此您需要将日期作为字符串或整数发送,并在适当时将它们转换为Date对象。 Angular does not do that for you, but you can define a default transformResponse function to $httpProvider . Angular不会为您执行此操作,但您可以将默认的transformResponse函数定义为$httpProvider I tried that with $resource and it does affect to it as well, after all, $resource is a higher level abstraction for $http . 我尝试使用$resource ,它确实对它有影响,毕竟, $resource$http的更高级抽象。

If you don't know the structure of your JSON or don't want to rely on it in the conversion function you just need to go it through and convert date-like strings to Date objects. 如果你不知道你的JSON的结构或者不想在转换函数中依赖它,你只需要通过它并将类似日期的字符串转换为Date对象。

var convertDates = function(input) {
  for(var key in input) {
    if (!input.hasOwnProperty(key)) continue;

    if (typeof input[key] === "object") {
      convertDates(input[key]);
    } else {
      if (typeof input[key] === "string" && /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$/.test(input[key])) {
        input[key] = new Date(input[key]);
      }
    }
  }
}

app.config(["$httpProvider", function ($httpProvider) {
   $httpProvider.defaults.transformResponse.push(function(responseData){
     convertDates(responseData);
     return responseData;
  });
}]);

Depending on your app this may be unwise from performance perspective (eg only a handful of JSON responses contain dates or actually need the dates to be converted) so it might be best to convert the dates in those controllers where that is actually needed. 根据您的应用程序,从性能角度来看这可能是不明智的(例如,只有少数JSON响应包含日期或实际上需要转换日期),因此最好转换那些实际需要的控制器中的日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM