2017-10-18 70 views
0

我有以下角度:角日期格式,如果条件

ngOnInit(){ 
    this.getInfo().then((data) => {    
     this.allInfo= data;   
    }); 
}; 

...其中data看起来象下面这样:

0: 
    Time: 1508218791131 
1: 
    Time: 1508217179413 
2: 
    Time: 1508217026810 

然后,在HTML:

<div *ngFor = "let each of allInfo;" > 
    <div>{{ each.Time | date : "shortTime" }}</div> 
</div> 

这些工作很好。

这里就是我想要做:

我想给Time比作current time有两个条件:

  1. 如果它比较了超过24小时到当前时间,然后用“shortTime”给出11:10 pm
  2. 如果它已经被更多比24小时比较到当前时间,然后使用“MMM d”给出Oct 16

如何将if条件添加到ngFor语法里面?

回答

2

你有可能的路径:

  • 修改this.allInfo有你想使用$过滤器控制器内,而不是直接在模板中的格式。看看Angular 2/4 use pipes in services and components
  • 使自定义过滤器,做你想做的。不推荐使用,因为过滤器旨在将表示逻辑封装在上下文中。