Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable a link in angular 2+

I'm attempting to disable a link until an API call is made in Angular 6. I want the link to be disabled until the getSelectedDealer() API is returned.

 <menu-link *ngIf="perms.has(perms.TOP_OFFERS) && _dealerPersistenceService.getSelectedDealer()"
           route="/dynamic-journeys/{{getDealerId()}}/vehicles">
    <menu-item><img src="/assets/menu-icons/top-offers.svg">Dynamic Journeys</menu-item>
</menu-link>

Here is the code for the 'a' tag component and the CSS.

<a [routerLink]="route" routerLinkActive="active" class="menu-link" [class.disabled]="disabled ? true: null">
<ng-content select="menu-item"></ng-content>

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

Basically I need the 'menu-link' items to be disabled prior to API call and be enabled after.

like image 880
LDB Avatar asked Jul 20 '18 15:07

LDB


People also ask

How do I deactivate a link?

To remove a hyperlink but keep the text, right-click the hyperlink and click Remove Hyperlink. To remove the hyperlink completely, select it and then press Delete.

How do I disable a link in HTML?

It is still possible to disable a link by following 3 steps: 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.

How do I make anchor tag disabled?

You can always use tabindex="-1" inside your anchor tag in order to disable it.


1 Answers

If you want to use no matter what, you could achieve that by simply add click event to this link with something like that:

<a [routerLink]="route"
   routerLinkActive="active"
   class="menu-link"
   [class.disabled]="disabled ? true: null"
   (click)="check($event)">

In TS:

isReady = false;
check = (event) => {
 if (!this.isReady) {
   event.preventDefault();
 }
}

So isReady is a boolean variable of your component, and it is false until you want to. When ever you click this link, it will check if it is true, but if it is false, it will prevent the original behavior of the event, and nothing will fire.

like image 170
dAxx_ Avatar answered Oct 01 '22 18:10

dAxx_