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
  • 58,546
  • 53
  • 279
  • 406

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
  • 58,546
  • 53
  • 279
  • 406
Munzer
  • 1,940
  • 2
  • 17
  • 23