0

How I can write a switch case or if statement in Angular that works for 3 options,

 if Boards.status==1 then "Approved"
 if Boards.status==2 then "Pending"
 if Boards.status==3 then "Rejected"

I have tried this but need to add all 3 options.

    <td>{{Boards.status==1 ? "Approved": "Pending" }}</td>
nik
  • 514
  • 2
  • 6
  • 19
  • 1
    You need enclose the second condition in parenthesis: `{{Boards.status==1 ? "Approved": (Boards.status==2?"Pending":"Rejected") }}` – Eliseo Apr 14 '20 at 08:41

2 Answers2

3

You could try this out:

<tr [ngSwitch=Boards.status]="switch_expression">
  <td *ngSwitchCase="1">Approved</td>
  <td *ngSwitchCase="2">Pending</td>
  <td *ngSwitchCase="3">Rejected</td>
  <td *ngSwitchDefault>...</td>
</tr>

Feel free to refer link

Arjun V
  • 197
  • 4
  • 16
0

You can't do that with the ternary operator, but there are a couple options. One is just to replace all the stuff between the interpolation operators with a function that returns the value you want, with the function being just some if/else statements. The other way would be a bunch of divs or ng-containers that used the ngif/else pattern *ngIf else if in template.

UPDATE: @Eliseo pointed out you actually can do this with the ternary operator. From the question you could do it like so I think:

<td>{{Boards.status == 1 ? "Approved": Boards.status === 2 ? "Pending": "Rejected" }}</td>

The accepted answer also has a good way that I forgot, which is by using ngSwitchCase

sovemp
  • 1,402
  • 1
  • 13
  • 31