简体   繁体   English

如何在 Angular 中(单击)后禁用按钮

[英]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我是 Angular 的新人,我想知道如何在单击同一个按钮并执行对应的操作后禁用该按钮

This is my button html code这是我的按钮 html 代码

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

You can bind the disabled property to a flag (eg clicked ), and set the flag in the click event handler:您可以将disabled属性绑定到一个标志(例如clicked ),并在click事件处理程序中设置该标志:

<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.有关演示,请参阅 此 stackblitz

<button type="button" class="btn btn-primary" (click)="actionMethod($event)">Give Kitchen access</button>
actionMethod(event: any) {
    event.target.disabled = true;
}

A template reference approach: 模板参考方法:

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

StackBlitz闪电战

Give Kitchen access 允许厨房访问

actionMethod() { actionMethod(){

TypeScript implementation打字稿实现

If you're using Angular, you're probably using TypeScript.如果您使用的是 Angular,那么您可能正在使用 TypeScript。 Here's the TypeScript implementation (inspired by @DanielWilliams' js implementation):这是 TypeScript 实现(受 @DanielWilliams 的 js 实现启发):

Component html组件html

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

Component ts组件 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.这,IMO,是卓越的解决方案,因为您不必在组件 ts 文件中保留布尔值,并且您可以获得 TypeScript 的类型。

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.例如,如果您有通过 *ngFor 循环在模板中生成的按钮,您可能只想禁用一个被单击的按钮。 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:在 html 模板中:

    <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:在 component.ts 添加:

    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 参见堆栈闪电战

You can also use css to disable events.您还可以使用 css 来禁用事件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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