简体   繁体   中英

How disable button after submit

I develop a form to add users, the form works, but the problem is when click on the button submit two successive times, the method add two users, I want the button submit (add) disable after click directly ( I work with angular 5)

HTML Code :

 <form [formGroup]="addfrm"> ... ... <div style="text-align:center"> <button class="btn btn-primary" (click)="processAdd()" [disabled]="addfrm.invalid">add</button> <button data-dismiss="modal" class="btn btn-default">cancel</button> </div> </form> 

You can try with ngForm directive :

<form [formGroup]="addfrm" #myform="ngForm">

  <div style="text-align:center">
    <button class="btn btn-primary" (click)="processAdd()" [disabled]="myform.submitted">add</button>
    <button data-dismiss="modal" class="btn btn-default">cancel</button>
  </div>

</form>

You can define a field within your component and set it to true when submitted.

<button class="btn btn-primary" (click)="processAdd()" [disabled]="addfrm.invalid || submitted">add</button>

within component

export class MyComponent {
    submitted = false;
    ...

    processAdd() {
        this.submitted = true; 
        this.someService.post(this.addForm).subscribe(result => {
            this.submitted = false; // reset it on response from server
        });
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM