【问题标题】:Why am I not able to disable mat-stroke-button like this?为什么我不能像这样禁用 mat-stroke-button?
【发布时间】:2019-05-19 20:15:35
【问题描述】:

我正在尝试基于 Angular Material 按钮构建自定义按钮。

问题是,由于某种原因,我不能再正确地disable 按钮了。样式等有效,但由于某种原因,button 中的内容似乎仍然适用于(click) 事件。

查看StackBlitz example,您可以在其中单击“禁用”以及“Spinner+Disabled”按钮。

HTML:

<button mat-stroked-button [color]="color" [disabled]="disabled || loading" [type]="type">
  <mat-icon *ngIf="icon" [class.hidden]="loading">{{icon}}</mat-icon>
  <span [class.hidden]="loading"><ng-content></ng-content></span>
  <div *ngIf="loading" class="spinner-wrapper">
    <mat-spinner [diameter]="20"></mat-spinner>
  </div>
</button>

组件:

import {Component, EventEmitter, HostListener, Input, Output} from '@angular/core';


@Component({
  selector: 'app-button',
  templateUrl: './button.component.html',
  styleUrls: ['./button.component.css']
})
export class ButtonComponent {

  @Input() icon: string;

  @Input() color = 'primary';

  @Input() disabled = false;

  @Input() type = 'submit';

  private _loading = false;

  @Input()
  set loading(loading: boolean) {
    this._loading = loading;
  }

  get loading(): boolean {
    return this._loading;
  }

}

样式:

:host {
  display: inline-grid;
}

.hidden {
  visibility: hidden;
}

.spinner-wrapper {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translateX(-50%) translateY(-50%);
}

知道如何防止这种情况发生吗?

【问题讨论】:

    标签: angular


    【解决方案1】:

    另一种解决方案是 CSS 指针事件:无

    ? button.component.css

    :host {
      display: inline-grid;
    }
    
    .hidden {
      visibility: hidden;
    }
    
    .spinner-wrapper {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translateX(-50%) translateY(-50%);
    }
    
    /* ######## add this pointer-events acting classes ####### */
    .disabled {
      pointer-events: all;
     }
    
    :host{
      pointer-events: none;
     }
    
    

    ? button.component.html

    <!-- and just add [class.disabled]="!disabled" to the button-->
    <button mat-stroked-button [color]="color" [disabled]="disabled || loading" [type]="type" [class.disabled]="!disabled">
      <mat-icon *ngIf="icon" [class.hidden]="loading">{{icon}}</mat-icon>
      <span [class.hidden]="loading"><ng-content></ng-content></span>
      <div *ngIf="loading" class="spinner-wrapper">
        <mat-spinner [diameter]="20"></mat-spinner>
      </div>
    </button>
    
    

    https://stackblitz.com/edit/stackoverflow-56211649

    【讨论】:

    • 有没有办法防止这种情况与app-button 组件发生冲突?
    • 现在检查 stackblitz。我稍后会改变我的答案。我认为它现在可以满足您的需要
    • 已更改。希望它对您的项目有所帮助并祝您好运:)
    • 是的,这是它应该做的。感谢您的帮助:)
    【解决方案2】:

    这是因为(click) 事件不在按钮上,而是在app-button 标签上。如果将点击处理程序传递给组件,并在按钮标签上设置(click) 属性,则禁用它不会触发。

    【讨论】:

    • 我意识到 StackBlitz 无法重现我机器上的内容。在我的机器上,不是整个按钮都是可点击的,只有图标/文本而不是它周围的区域。这就是让我感到困惑的地方。我会尝试重现它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-27
    • 2017-01-12
    • 2015-09-09
    • 2012-04-22
    • 2012-08-31
    相关资源
    最近更新 更多