99

In my Angular 2 application I'm trying to disable a routerLink without any success. I've tried to handle the click event on the click event (with event.preventDefault() and event.stopPropagation()) but it doesn't work.

How can I disable a routerLink?

isherwood
  • 58,414
  • 16
  • 114
  • 157
TizianoL
  • 1,271
  • 2
  • 11
  • 13
  • 1
    This is such a massive waste of thousands and thousands of hours trying to find a solution that could be solved with about 5 lines of code in Angular :-( Could be as simple as `[routerLinkClickEnabled]="false"` and done! – Simon_Weaver Nov 20 '19 at 06:58
  • @Simon_Weaver: Could you please provide a docu link for `[routerLinkClickEnabled]`? My searches are unsuccessful. – ruth Feb 10 '20 at 12:24
  • 1
    @MichaelD this was a suggestion of what they *could* do to fix the problem and not something that actually exists. sorry I wasn't clearer! Hopefully now Angular 9 is out they'll be able to loop back on certain common issues like this one. – Simon_Weaver Feb 10 '20 at 19:27
  • 1
    @Simon_Weaver: I overlooked the '_could be_' phrase in your original comment. All clear now. Thanks. – ruth Feb 11 '20 at 10:09

8 Answers8

126

Disable pointer-events on the element via CSS:

<a [routerlink]="xxx" [class.disabled]="disabled ? true : null">Link</a>

a.disabled {
   pointer-events: none;
   cursor: default;
}

See also Angular2, what is the correct way to disable an anchor element?

or

<a *ngIf="isEnabled" [routerlink]="xxx">Link</a>
<div *ngIf="!isEnabled">not a link</div>

or to easily reuse the disabled link template

<ng-template #disabledLink>
  <div *ngIf="!isEnabled">not a link</div>
</ng-template>
<a *ngIf="isEnabled; else disabledLink" [routerLink]="xxx">Link</a>
Bellash
  • 7,560
  • 6
  • 53
  • 86
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • 2
    Thank you, it works. I'm using bootstrap to add style to the navbar. Your solution removes the `not-allowed` cursor style when the link is disabled. I had to add it separately. – TizianoL Feb 16 '16 at 13:43
  • 1
    The `cursor` line can be omitted entirely, it's just the visual cue that the link is disabled. I bootstrap sets that already, use this setting instead. – Günter Zöchbauer Feb 16 '16 at 14:18
  • Doesn't work for internet explorer and Edge - it's still clickable – user3292624 Nov 30 '17 at 14:40
  • You can use `*ngIf` to show alternative content instead of trying to make the ` – Günter Zöchbauer Nov 30 '17 at 14:43
  • 2
    @user3292624 still, is IE considered a browser? – Vignesh Jan 19 '18 at 13:55
  • In a wider sense of the word, I guess so :D – Günter Zöchbauer Jan 19 '18 at 13:58
  • 1
    Also useful if you want to disable the current active route: use routerLinkActive. Add pointer-events: none of the active class. – Hayden Braxton Feb 24 '20 at 21:19
  • Do not work. If router link is empty: it redirects to default route. (angular 8) – Damien C Apr 23 '20 at 08:59
  • @DamienC This answer does not say that an empty routerLink would solve the issue. Perhaps I didn't understand your comment. – Günter Zöchbauer Apr 23 '20 at 09:29
  • 1
    Thanks @GünterZöchbauer . to manage conditional link, finally, I manage to do it only in controller. Unable to do it directly in template. But in a architecture point of view, it's a better way to code it – Damien C Apr 24 '20 at 15:18
  • If you use the second example and are using routerLinkActive on an ancestor element, you will find that the *ngIf will block the [routerLink] from being seen and thus routerLinkActive will fail to work as intended. To work around this, just add an empty anchor element. ```
  • Link Link
  • ``` – Kerry Johnson May 08 '20 at 01:06