我的目标是过滤对象内的数组,然后将它们列在表格上。
我创建了一个过滤器,它根据日期迭代对象并过滤数据。但我的过滤器触发了$digest loop
异常。
代码有什么问题或我该如何实现?
完整代码: https://plnkr.co/edit/y4AqW6xjy79MVEhu7zfo?p=preview
过滤
.filter('dateRange',
function($linq, moment) {
return function(items, startDate, endDate) {
var output, sDate, eDate;
output = [];
sDate = startDate ? moment(startDate) : moment(0);
eDate = endDate ? moment(endDate) : moment();
if (items._filtered) {
return items; //never reaches here
}
if (!sDate.isValid() || !eDate.isValid()) {
console.warn('Invalid date format.');
return items || [];
}
angular.forEach(items, function(item, i) {
var car, fS;
car = angular.copy(item);
fS = $linq.Enumerable()
.From(item.fuelSupplies || [])
.Where(function(x) {
var fueledDate = moment(x.fueledAt);
if (!fueledDate.isSameOrAfter(sDate) || !fueledDate.isSameOrBefore(eDate)) {
return false;
}
return true;
});
car.fuelSupplies = fS.ToArray();
output.push(car);
});
output._filtered = true;
return output;
};
});
用法:
<tr ng-repeat="x in cars | dateRange : '2016/6/31': '2016/11/31'">
<td class="text-center">{{x.licencePlate}}</td>
<td>{{x.model}}</td>
<td>{{x.manufacturer}}</td>
<td class="text-center">{{x.getFuelAverage() | number : 2}}</td>
<td class="text-center">{{x.getCostAverage() | currency}}</td>
</tr>