我有以下型号,并希望显示过去24小时内投票的最高价
{
"_id": {
"$oid": "56abe20fabd3fa64187b5d21"
},
"user": {
"$oid": "56a1832a36dc3d0e00c7aa3f"
},
"title": "Intel Pentium i5 Quad-Core i5-6400",
"created": {
"$date": "2016-01-29T22:05:03.574Z"
},
"votes": 1
}
所以基本上在过去24小时内将ng-repeat限制为创建的字段。
过滤
angular.module('savings').filter('lessThan', function () {
return function(savings, requirement) {
var filterKey = Object.keys(requirement)[0];
var filterVal = requirement[filterKey];
var filtered = [];
if(filterVal !== undefined && filterVal !== ''){
angular.forEach(savings, function(saving) {
var today = new Date();
var date = new Date(saving.created.$date);
var diff = today - date;
diff = diff / (1000*60*60);
if(diff < filterVal) {
filtered.push(saving);
}
});
alert(filtered.length);
return filtered;
}
return savings;
};
});
我已尝试实施以下答案,但它不会返回任何结果。
资源截图。
答案 0 :(得分:2)
我会节省你一些时间..这是你的过滤器。
app.filter('lessThan', function () {
return function(items, requirement) {
var filterKey = Object.keys(requirement)[0];
var filterVal = requirement[filterKey];
var filtered = [];
if(filterVal !== undefined && filterVal !== ''){
angular.forEach(items, function(item) {
var today = new Date();
var date = new Date(item.created)
var diff = today - date;
diff = diff / (1000*60*60)
if(diff < filterVal) {
filtered.push(item);
}
});
return filtered;
}
return items;
};
});
并称之为:
<div ng-repeat="item in items | lessThan: {'created':24}">
Here是一名掠夺者。
编辑:根据截图创建的属性看起来更像是这样:
"created": "2016-01-29T22:05:03.574Z"
我已编辑此模型的过滤器