javascript - Angular 8:隐藏过去事件的管道不起作用
问题描述
bookingEnd
我在模板中创建了一个复选框,根据我的 JSON 数据隐藏所有早于今天发生的元素。recent-filter.pipe.ts
管道应该过滤所有过去的事件。
对于我的问题,我error TS2365: Operator '>' cannot be applied to types 'number' and 'Date'.
提前在管道中获得了一个,并且没有数据显示在我的模板上。整个场景在纯 JavaScript 中工作,所以我认为这bookingEnd
绝对是一个日期对象。
你能帮我为什么应用管道过滤器后没有数据吗?
JSON数据:
{
bookingStart: "2019-10-27T23:00:00.000Z",
bookingEnd: "2019-12-29T23:00:00.000Z",
city: "Manhattan",
confirmed: false,
country: "UK"
}
最近-filter.pipe.ts:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'recentFilter'
})
export class RecentFilterPipe implements PipeTransform {
transform(list: any[], checked: boolean) {
if (checked) {
const futureDates = list.filter(x => {
return Date.parse(x.bookingEnd) > new Date();
});
return futureDates;
} else {
return list;
}
}
}
预订表.component.html:
<tr
*ngFor="
let item of bookings
| recentFilter: checked
">
...
</tr>
解决方案
您得到的错误已经告诉您不要使用 > 来比较日期
要比较它们,请使用 .getTime() 方法
new Date(x.bookingEnd).getTime() > new Date().getTime()
推荐阅读
- c++ - 在 Omnet++ 的模拟期间更改/设置节点的新位置
- javascript - 在反应中动画(获取数据)后不是组件渲染
- selenium-chromedriver - 我在定位 div 标签内的输入标签时遇到问题
- office-js - setSolidColor 在图表系列的 Excel Javascript 中引发意外错误
- python - 什么不是如果,它与字符串有什么关系
- java - 在 Java Vert.x 上处理发布请求的有效方法?
- c# - How to avoid multiple queries one complex objects in EF .net core
- angular - 在 Ionic 4 中导入 Ionic-Native File 插件
- excel - 将 countif / countifs 范围设置为仅范围中的最后一行
- uwsgi - 将 uWSGI 配置为不解释 PATH_INFO