我正在构建我的第一个角度应用。
我正在使用包含足球比赛装置的工厂服务从API返回一些JSON数据。
我想按日期分组,但忽略时间。 JSON日期字符串的格式为" 2016-06-10T19:00:00Z"
我设法在我的ng-repeat上使用groupBy,这很正常 - 唯一的问题是 - 它也考虑了时间,因此在不同KO时间的同一天的2场比赛将不会被分组。< / p>
我的JSON对象是这样的,我将它分配给我的控制器中名为fixtures.list的数组:
"fixtures":[
{
"_links":{
"self":{
"href":"http://api.football-data.org/v1/fixtures/149855"
},
"soccerseason":{
"href":"http://api.football-data.org/v1/soccerseasons/424"
},
"homeTeam":{
"href":"http://api.football-data.org/v1/teams/773"
},
"awayTeam":{
"href":"http://api.football-data.org/v1/teams/811"
}
},
"date":"2016-06-10T19:00:00Z",
"status":"TIMED",
"matchday":1,
"homeTeamName":"France",
"awayTeamName":"Romania",
"result":{
"goalsHomeTeam":null,
"goalsAwayTeam":null
}
},
...
和我的角度模板如下:
http://plnkr.co/edit/R4qjDfyKhefDOaHmleJv?p=preview
我的控制器:
function FixturesController (EurosService) {
var ctrl = this;
ctrl.list = [];
function getFixtures () {
EurosService
.retrieveFixtures()
.then( function (response) {
ctrl.list = response;
});
}
getFixtures();
}
angular
.module('app')
.controller('FixturesController', FixturesController)
对此表示感谢。什么是最好的方法呢?感谢。
答案 0 :(得分:1)
与此回答相关https://stackoverflow.com/a/30509960/1211299
您可以向群组过滤器添加映射功能:| map: toLocaleDate | groupBy: 'date'
你的映射函数是这样的:
$scope.toLocaleDate = function(e) {
e.date = e.date.toLocaleDateString();
return e;
};
此函数将您的日期转换为仅日期字符串。时间部分将从原始对象中剥离。