根据与当前时间相比的时间戳筛选 Angular 2 中的字段

Filter fields in Angular 2 based on timestamp compared with current time

我正在构建一个显示对象列表的应用程序。每个对象都有一个 archived_until 属性,可以保存值 null 或不带时区的时间戳(例如:2016-11-26 [=29= .561139).

我正在尝试显示所有这些对象的列表,并让用户可以切换名为 Show archived objects 的按钮。我想知道将 Angular 2 中的时间戳与当前日期进行比较的最佳方法是什么?我可以在模板中使用它还是应该在我的组件中编写一个函数?

这是我迄今为止尝试过的方法,它没有 return 任何错误,但也没有按预期进行过滤(它什么也没显示)。我添加了一个按钮来切换 show_archived_notes:

的值
<tr *ngFor="let notification of notifications">
    <ng-container 
     *ngIf=(!show_archived_notes && !notification.archived_until) || 
     notification.archived_until > currentDate>
        <td>Some attribute</td><td>Another attribute</td>...
    </ng-container>
</tr>

我在组件中初始化 currentDate 的位置如下:

current_date = new Date();

您可以使用更简单的方法,编写一个管道来过滤存档并添加一个活动参数:

<tr *ngFor="let notification of notifications | archivePipe:show_archived_notes">

管道将获取数组,如果 active 为 false,return 数组原样,如果不是,则按您的逻辑过滤它。

@Pipe({
    name: 'archivePipe'
})
export class ArchivePipe implements PipeTransform {
  transform(notes: Array<Notes>, active: boolean): Array<Notes> {
      if(active){
          return notes.filter(<your archive filter logic>);
      } else {
          return notes;
      }
  }
}