Angular.js格式从json对象开始 [英] Angular.js format date from json object
问题描述
我的json响应如下所示:
My json response look like this:
[{"Id":"dab4580b-e24d-49f8-9fd5-2e968b10d3b5","Title":"MVVM-Sidekick 入精","CreatedOn":"\/Date(1390272893353)\/","IsChecked":false},{"Id":"66a0f134-e240-4cc4-96fa-ac3807853ca7","Title":"Windows Phone 开发入精","CreatedOn":"\/Date(1390018447080)\/","IsChecked":false}]
CreatedOn日期采用这种格式:'/ Date(1390272893353)/'
the "CreatedOn" date is in this kind of format: '/Date(1390272893353)/'
当我将此结果绑定到html表时,日期无法格式化:
when I bind this result to html table, the date cannot be formatted:
<td>{{item.CreatedOn | date: 'yyyy-MM-dd HH:mm'}}</td>
仍然给我:
/日期(1390272893353)/
/Date(1390272893353)/
我不想更改服务器端的任何代码(不要修改json字符串),格式化日期的最好方法是什么
I don't want to change any code on the server side (don't modify the json string), what's the best way to format this date?
推荐答案
一个选项是写另一个过滤器并将其放在链中。例如:
One option is to write another filter and put it in the chain. E.g.:
app.filter("mydate", function() {
var re = /\/Date\(([0-9]*)\)\//;
return function(x) {
var m = x.match(re);
if( m ) return new Date(parseInt(m[1]));
else return null;
};
});
基本上它使用正则表达式来解析字符串,并使 Date
(如果格式与显示的格式不同,您将不得不调整正则表达式)。
Basically it uses the regular expression to parse the string and make a Date
(if the format is different than the one shown, you will have to tweak the regular expression).
使用它作为:
<td>{{item.CreatedOn | mydate | date: 'yyyy-MM-dd HH:mm'}}</td>
这篇关于Angular.js格式从json对象开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!