31

I am new at Angular, and I would like to know how to disable a button after I click that same button and performed the action correspondent

This is my button html code

<button type="button" class="btn btn-primary" (click)="actionMethod()">Give Kitchen access</button> <br> <br>

6 Answers 6

55

You can bind the disabled property to a flag (e.g. clicked), and set the flag in the click event handler:

<button type="button" [disabled]="clicked" (click)="actionMethod(); clicked = true;" class="btn btn-primary">Give Kitchen access</button>

The flag should be declared in the component class:

clicked = false;

See this stackblitz for a demo.

Sign up to request clarification or add additional context in comments.

3 Comments

I have form validations bound to the button ng-disabled="!validObject" But even if the validation passes after first click, I need to disable the button, how can I do this?
But if I need to use it in more places then its get pretty unhandy? One bool for different buttons or one bool for a component? And whats about booleans across components?
@liqSTAR - To reuse this kind of button, make it a component and define the flag in that component.
28
<button type="button" class="btn btn-primary" (click)="actionMethod($event)">Give Kitchen access</button>
actionMethod(event: any) {
    event.target.disabled = true;
}

2 Comments

Small addition, if you have something like <button (click)="actionMethod($event)"><span>Click here</span></button> it might be possible that the target element refers to the span, not to the button. So when using sub-elements in a button, event.currentTarget.disabled = true can be helpful.
@StevenX suggestion worked for me (can't use a flag because the button is in a list and I just want to disable the clicked one, not the others). All other attempts to try and get the target, cast, closest, etc. failed for me. PS: yeah. it was a button with a span.
9

TypeScript implementation

If you're using Angular, you're probably using TypeScript. Here's the TypeScript implementation (inspired by @DanielWilliams' js implementation):

Component html

<button type="button" class="btn btn-primary" (click)="actionMethod($event)">Give Kitchen access</button>

Component ts

actionMethod($event: MouseEvent) {
    ($event.target as HTMLButtonElement).disabled = true;
    // Do actions.
}

This, IMO, is the superior solution because you don't have to keep booleans around in your Component ts files and you get TypeScript's types.

1 Comment

The last comment is always the best. It helped me a lot because I can't know the amount of buttons I will have.
6

A template reference approach:

    <button (click)="submit();submitButton.disabled = true" #submitButton>submit</button>

StackBlitz

Comments

0

If you have buttons generated in template by a *ngFor loop, for example, you may want to disable only one button that was clicked on. For that you have to idendify which one was clicked on. You could store clicked buttons in array. If button is clicked on, it's pushed to array, to disable it. After specified time you can remove the button from array, to enable it.

In html template:

    <ng-container *ngFor="let button of buttons">
      <button mat-stroked-button
        #button
        (click)="disableButton(button)"
        [disabled]="disabledButtons.includes(button)">
      </button>
   </ng-container>

In component.ts add:

    buttons = [1,2,3]
    disabledButtons = [];
      // Disables click button for period of 2 seconds
      disableButton(button: any) {
        this.disabledButtons.push(button);
        setTimeout(() => {
          this.disabledButtons.splice(this.disabledButtons.indexOf(button), 1);
        },2000)
      }

See stackblitz

Comments

-2

You can also use css to disable events.

[ngStyle]="{'pointer-events': (condition) ? 'none' : 'all'}"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.