gpt4 book ai didi

Angular 分量: disable click event

转载 作者:行者123 更新时间:2023-12-02 14:09:24 29 4
gpt4 key购买 nike

我正在创建一个像这样的可重用组件:

<my-button [isDisabled]="isDisabled" (click)="click($event)"> submit </my-button>

我想在属性 isDisabled 为 true 时禁用点击事件,我尝试了类似的操作,但它不起作用。

packages/component/my-button.component.html

<button  [disabled]="isDisabled" #myButton>
<ng-content></ng-content>
</button>

packages/component/my-button.component.ts

@ViewChild('uxButton') uxButton: ElementRef;
@Input() isDisabled: boolean = false;

this.myButton.nativeElement.parentNode.removeEventListener('click' , (e) => {
e.stopPropagation();
});

最佳答案

也可以通过以下CSS解决:

# This prevents host to get a direct click
:host {
pointer-events: none;
}

# This prevents the span inside the button to "steal" the click from the button
:host /deep/ button span {
pointer-events: none;
}

# Now only the button can get a click
# Button is the only smart enough to stop propagation when needed
button {
pointer-events: auto;
}

现在您不需要像其他答案一样手动传递点击事件:您已经恢复了旧的(点击)事件:D

<my-button [isDisabled]="isDisabled" (click)="click($event)"> submit </my-button>

在您的自定义组件中,您只需传递禁用的属性即可:

<button  [disabled]="isDisabled" #myButton>
<ng-content></ng-content>
</button>

另请考虑stackblitz修改自another stackoverflow answer .

关于 Angular 分量: disable click event,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55402388/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com