【问题标题】:Google Pay Button Disable on ConditionGoogle Pay 按钮在条件下禁用
【发布时间】:2021-11-22 16:22:59
【问题描述】:

我需要在 Angular 中禁用 google-pay-button。以下是stackblitz链接

Stackblitz

我试过 [disabled]、[attr.disabled]。但似乎没有任何效果。在某些情况下,我必须禁用此 google pay 按钮。非常感谢您的帮助。

Google Pay Button Docs

【问题讨论】:

    标签: angular typescript google-pay


    【解决方案1】:

    我们可以使用指令扩展 Google Pay Button 组件的功能。在这种情况下,我们可以有一个 disabled 指令,它可以在需要禁用组件属性时接受它。

    创建一个 gpayDisabled 指令:

    import { Directive, ElementRef, Input } from '@angular/core';
    import { interval } from 'rxjs';
    
        @Directive({
          selector: '[gpayDisabled]',
        })
        export class GpayDisabledDirective {
          @Input() gpayDisabled = false;
          constructor(el: ElementRef) {
            const btnTimer = interval(200).subscribe(() => {
              let gPayButton = el.nativeElement.querySelector(
                '.gpay-card-info-container'
              );
              if (gPayButton) {
                 gPayButton.disabled = this.gpayDisabled;
                 if (this.gpayDisabled) {
                  gPayButton.style.opacity = 0.5;
                }
                btnTimer.unsubscribe();
              }
            });
          }
        }
    

    在模块的声明数组中声明这个,例如:

    declarations: [AppComponent, GpayDisabledDirective]
    

    然后在模板中,我们可以像这样使用这个指令:

     <google-pay-button
        [gpayDisabled]="isGpayDisabled"
        environment="TEST"
        [buttonColor]="buttonColor"
        [buttonType]="buttonType"
        [buttonSizeMode]="isCustomSize ? 'fill' : 'static'"
        [paymentRequest]="paymentRequest"
        [style.width.px]="buttonWidth"
        [style.height.px]="buttonHeight"
        (loadpaymentdata)="onLoadPaymentData($event)"
      ></google-pay-button>
    

    其中isGpayDisabled 是组件属性,为真或假。

    Stackblitz 演示。

    【讨论】:

    • 这使它不可点击,但在按钮中感觉不到禁用的感觉,这意味着按钮颜色保持不变的黑色而不是变灰。
    • 是的,检查更新的指令代码:)。也注意到了这一点并更新了代码以使其看起来被禁用。
    • 谢谢。它奏效了。
    猜你喜欢
    • 2021-08-31
    • 2014-10-14
    • 2013-01-27
    • 1970-01-01
    • 2017-09-17
    • 1970-01-01
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多