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

HtmlDomTypescriptAngularAngular2 Template

Html Problem Overview


I'm working on an Angular2 application, and I need to display -- but disable an <a> HTML element. What is the correct way to do this?

Updated

Please note the *ngFor, this would prevent the option of using *ngIf and not rendering the <a> altogether.

<a *ngFor="let link of links"
   href="#" 
   [class.disabled]="isDisabled(link)" 
   (click)="onClick(link)">
   {{ link.name }}
</a>

The TypeScript component has a method that looks like this:

onClick(link: LinkObj) {
    // Do something relevant with the object... 
    return false;
}

I need to actually prevent the element from being clickable, not just appear that it is with the CSS. I was assuming that I needed to potentially bind to the [disabled] attribute at first, but this is incorrect as the anchor element doesn't have a disabled property.

I looked at and considered using the pointer-events: none but this prevents my style of cursor: not-allowed from working -- and this is part of the requirement.

Html Solutions


Solution 1 - Html

Specifying pointer-events: none in CSS disables mouse input but doesn't disable keyboard input. For example, the user can still tab to the link and "click" it by pressing the Enter key or (in Windows) the ≣ Menu key. You could disable specific keystrokes by intercepting the keydown event, but this would likely confuse users relying on assistive technologies.

Probably the best way to disable a link is to remove its href attribute, making it a non-link. You can do this dynamically with a conditional href attribute binding:

<a *ngFor="let link of links"
   [attr.href]="isDisabled(link) ? null : '#'"
   [class.disabled]="isDisabled(link)"
   (click)="!isDisabled(link) && onClick(link)">
   {{ link.name }}
</a>

Or, as in Günter Zöchbauer's answer, you can create two links, one normal and one disabled, and use *ngIf to show one or the other:

<ng-template ngFor #link [ngForOf]="links">
    <a *ngIf="!isDisabled(link)" href="#" (click)="onClick(link)">{{ link.name }}</a>
    <a *ngIf="isDisabled(link)" class="disabled">{{ link.name }}</a>
</ng-template>

Here's some CSS to make the link look disabled:

a.disabled {
    color: gray;
    cursor: not-allowed;
    text-decoration: underline;
}

Solution 2 - Html

For [routerLink] you can use:

Adding this CSS should do what you want:

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

This should fix the issue mentioned by @MichelLiu in the comments:

<a href="#" [class.disabled]="isDisabled(link)"
    (keydown.enter)="!isDisabled(link)">{{ link.name }}</a>

Another approach

<a [routerLink]="['Other']" *ngIf="!isDisabled(link)">{{ link.name }}</a>
<a  *ngIf="isDisabled(link)">{{ link.name }}</a>  

Plunker example

Solution 3 - Html

My answer might be late for this post. It can be achieved through inline css within anchor tag only.

<a [routerLink]="['/user']" [style.pointer-events]="isDisabled ?'none':'auto'">click-label</a>

Considering isDisabled is a property in component which can be true or false.

Plunker for it: https://embed.plnkr.co/TOh8LM/

Solution 4 - Html

Just came across this question, and wanted to suggest an alternate approach.

In the markup the OP provided, there is a click event binding. This makes me think that the elements are being used as "buttons". If that is the case, they could be marked up as <button> elements and styled like links, if that is the look you desire. (For example, Bootstrap has a built-in "link" button style, https://v4-alpha.getbootstrap.com/components/buttons/#examples)

This has several direct and indirect benefits. It allows you to bind to the disabled property, which when set will disable mouse and keyboard events automatically. It lets you style the disabled state based on the disabled attribute, so you don't have to also manipulate the element's class. It is also better for accessibility.

For a good write-up about when to use buttons and when to use links, see Links are not buttons. Neither are DIVs and SPANs

Solution 5 - Html

consider the following solution

.disable-anchor-tag { 
  pointer-events: none; 
}

Solution 6 - Html

   .disabled{ pointer-events: none }

will disable the click event, but not the tab event. To disable the tab event, you can set the tabindex to -1 if the disable flag is true.

<li [routerLinkActive]="['active']" [class.disabled]="isDisabled">
     <a [routerLink]="['link']" tabindex="{{isDisabled?-1:0}}" > Menu Item</a>
</li>

Solution 7 - Html

This is for anchor tags that act as tabs:

[ngStyle]="{'pointer-events': isSaving ? 'none': 'auto'}"

Solution 8 - Html

I have used

tabindex="{{isEditedParaOrder?-1:0}}" 
[style.pointer-events]="isEditedParaOrder ?'none':'auto'" 

in my anchor tag so that they can not move to anchor tag by using tab to use "enter" key and also pointer itself we are setting to 'none' based on property 'isEditedParaO rder' whi

Solution 9 - Html

instead of class.disabled, we can also bind a variable with the disabled attribute. This will be used only buttons, inputs and dropdowns. HTML disabled Attribute

For Example:

<button [disabled]="isDisabled">Test</button>

Demo

Solution 10 - Html

You can try this

<a [attr.disabled]="someCondition ? true: null"></a>

Solution 11 - Html

Just use

<a [ngClass]="{'disabled': your_condition}"> This a tag is disabled</a>

Example:

 <a [ngClass]="{'disabled': name=='junaid'}"> This a tag is disabled</a>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavid PineView Question on Stackoverflow
Solution 1 - HtmlMichael LiuView Answer on Stackoverflow
Solution 2 - HtmlGünter ZöchbauerView Answer on Stackoverflow
Solution 3 - Htmlvineetk27View Answer on Stackoverflow
Solution 4 - HtmlVan J. WilsonView Answer on Stackoverflow
Solution 5 - HtmlSahil RalkarView Answer on Stackoverflow
Solution 6 - HtmlDarin CardinView Answer on Stackoverflow
Solution 7 - HtmlHenryView Answer on Stackoverflow
Solution 8 - HtmlguestView Answer on Stackoverflow
Solution 9 - HtmlVigneshView Answer on Stackoverflow
Solution 10 - HtmlVincent ShenView Answer on Stackoverflow
Solution 11 - HtmlPullat JunaidView Answer on Stackoverflow