Having a dash if field values are null

Viewed 670

I am working on an angular application. I have following code in my html

      <div>{{ data.date | date:'d-MMM-y' || '-' }}</div>
       <div>{{ data.id  || '-' }}</div> 

The problem I am facing is, whenever for any field like date and id, if value coming from API is null then next div shifts upward. I want div to be in that particular place it is when value is not null. So, I want to have a dash "-" whenever a value coming from API is null. So in my code I added a || '-' to all the fields but still it is not working. I am not getting "-" in my html. How can I do that?

2 Answers

You can use on this case the nullish coalescing operator

it will be like:

  <div> {{ (data.date | date:'d-MMM-y') ?? '-' }} </div>
  <div> {{ data.id ?? '-' }} </div>
Related