Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 239
David Pine Avatar asked May 02 '16 14:05

David Pine


People also ask

How do I turn off the anchor element?

To disable a HTML anchor element with CSS, we can apply the pointer-events: none style. pointer-events: none will disable all click events on the anchor element. This is a great option when you only have access to class or style attributes. It can even be used to disable all the HTML links on a page.

How do you disable a tag?

The <a> tag doesn't have a disabled attribute, that's just for <input> s (and <select> s and <textarea> s). To "disable" a link, you can remove its href attribute, or add a click handler that returns false.

How do I disable a link in HTML?

Disable a link #remove the href attribute so that it can no longer receive the focus. add a role="link" so that it is always considered a link by screen readers. add an attribute aria-disabled="true" so that it is indicated as being disabled.


2 Answers

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; } 
like image 176
Michael Liu Avatar answered Oct 12 '22 20:10

Michael Liu


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

like image 21
Günter Zöchbauer Avatar answered Oct 12 '22 21:10

Günter Zöchbauer