69

In Angular 2 if I have an element like <button></button> how can I conditionally add an attribute directive like [routerLink]="['SomeRoute'] to it?

rcarrington
  • 1,315
  • 2
  • 11
  • 22

8 Answers8

171

Or you can simply add a condition to the attribute.

<button [routerLink]="myVar ? ['/myScreen'] : []"></button>

Redirect to '/myScreen' only if myVar is true.

fifix
  • 1,743
  • 2
  • 7
  • 6
47

As far as I know, there is no straight way to do this. There are some workarounds... I used something like this:

<button *ngIf="condition" [routerLink]="['SomeRoute']"></button>
<button *ngIf="!condition"></button>

There is an similar discussion here: link

Tomas Vancoillie
  • 2,854
  • 2
  • 30
  • 37
Boies Ioan
  • 794
  • 7
  • 11
10

If you don't want to duplicate the element, and just want to prevent clicks depending on the condition, you could do the following:

<button 
  [style.pointer-events]="condition ? 'auto' : 'none'" 
  routerLink="/some/route"
>
</button>
Jonathan Dudley
  • 109
  • 1
  • 4
3
   <div [ngClass] ='{"disabled-link":!isMicrositeEnable,"cursor-pointer":"isMicrositeEnable"}' [routerLink]="isMicrositeEnable ? ['/microsite'] : []">
Ashutosh Jha
  • 13,573
  • 9
  • 48
  • 77
3

This is a working sample. When you need to bring more than one condition you can have inner condition as follows

<a class="dropdown-item" href="javascript:void(0);" [routerLink]="app.treatment === 'X' ? ['/route1', app._id] : app.treatment === 'Y' ? ['/route2', app._id] : ['/route3', app._id] ">Go</a>
Syscall
  • 18,131
  • 10
  • 32
  • 49
0

Just a detail. Try to avoid using buttons for navigation. Screenreaders wont understand that its a navigation out of the box. You will now need to add an aria-label that tells the screenreaders what it does. This adds more code. But the simple solution would be to add the [routerLink] to an anchor link. Then style it as a button. But I prefer to use the standard blue links because people usually knows what will happen then. ex: I would never try to rightclick on a button and open it in a new tab. So: buttons for operations and anchorlinks for navigation

Jens Alenius
  • 1,832
  • 2
  • 15
  • 19
  • This is a misleading answer. Buttons can be used for navigation since navigation does not always mean an element is a link. It's perfectly ok to use buttons for dropdown navigation, popup menus, etc. (that's still "navigation"). – SimonRabbit Jan 04 '21 at 14:51
  • @SimonRabbit how do you make your buttons accessible for screen readers? – Frazer Kirkman Mar 30 '21 at 22:38
0

This worked for me in Angular 10:

<button routerLink="{{MyVar == true ? '/route/' + item.id : '/same/route'}}"></button>
0

I would not use a button with routerLink. A would use an anchorlink styled as a button to do it. The button element is for operations and anchors is for routing/navigate. Below code will tell the browser/screenreaders etc. what it is.

<a *ngIf="condition" class="make_it_look_like_a_button" [routerLink]="['somehere']">test</a>
<button *ngIf="!condition">Test</button>

The link case above will have 'open in new tab...' when right clicking it. But a drawback is that hardly no one would think of it when it is style as a button.

Jens Alenius
  • 1,832
  • 2
  • 15
  • 19