Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable button after (click) in Angular

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>
like image 926
José Nobre Avatar asked Sep 26 '18 00:09

José Nobre


2 Answers

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.

like image 110
ConnorsFan Avatar answered Oct 22 '22 11:10

ConnorsFan


<button type="button" class="btn btn-primary" (click)="actionMethod($event)">Give Kitchen access</button>
actionMethod(event: any) {
    event.target.disabled = true;
}
like image 22
Daniel Williams Avatar answered Oct 22 '22 11:10

Daniel Williams