0

I have following angular:

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

...where the data looks like below:

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

Then, in html:

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

These works just fine.

Here is what I am trying to do:

I am trying to compare the Time to the current time with two conditions:

  1. If it has been less than 24 hours compared to the current time, then use "shortTime" giving 11:10 pm
  2. If it has been more than 24 hours compared to the current time, then use "MMM d" giving Oct 16.

How do I add the if condition to the syntax inside of ngFor?

Steve Kim
  • 5,293
  • 16
  • 54
  • 99

1 Answers1

2

You have to possible paths:

  • modify this.allInfo to have the format that you want using $filter inside the controller instead that directly inside the template. Look at Angular 2/4 use pipes in services and components.
  • make a custom filter that does what you want. Not recommended since filters are meant to encapsulate presentation logic across contexts.
ichigolas
  • 7,595
  • 27
  • 50