0

Do you know how to use the ternary operator with routerLink?

Now it is like so:

<ion-button *ngIf="event?.evId"
      routerLink="/event/event-details/{{event?.evId}}">
            Cancel
</ion-button>

<ion-button *ngIf="!event?.evId" routerLink="/event"> Cancel
 </ion-button>

I would like to use the ternary operator here and remove one section completely. How to do that?

Note I can do that inside the TS file. But how to do that without using the TS file?

Sampath
  • 63,341
  • 64
  • 307
  • 441

1 Answers1

4

OP's

  <ion-button [routerLink]="event?.evId ? '/event/event-details/'+ event?.evId : '/event'"> 
       Cancel
   </ion-button>

Original

You can bind to any attribute like this

[routerLink]="event?.evId != null ? '/event/event-details/'+event.evId : '/event' "
Sampath
  • 63,341
  • 64
  • 307
  • 441
Munzer
  • 2,216
  • 2
  • 18
  • 25