[英]Button click event issue in angular
am我正在嘗試對自定義按鈕應用“禁用選項”,但是當我將按鈕設置為“禁用”時,單擊事件仍然有效。 如果按鈕已啟用,則僅需要click事件,如果按鈕被禁用,則不需要。
碼:
<myngbutton [disabled]="disabled3" (click)="alertMsg()">Save 3</myngbutton>
由於您的myngbutton
不是html本機元素,因此不會像本機按鈕那樣工作。 它被視為具有父類型HtmlElement的自定義元素。
disabled
html屬性僅適用於W3School文檔中描述的元素。 這意味着,如果您希望組件像按鈕一樣工作,則需要對其進行編程。 或在組件內部使用本機<button>
。
這是一個例子:
<!-- Won't enter the alertMsg method if it is disabled -->
<myngbutton [disabled]="disabled3" (click)="!disabled3 && alertMsg()">Save 3</myngbutton>
問題是,是, click
你訂閱的事件,是不是click
按鈕元素上的事件,但在點擊myngbutton
元素。 您需要在myngbutton
組件上添加一個事件發射器,該事件發射器在button
元素單擊時發出:
myngbutton.component.ts:
@Component({...})
export class MyngbuttonComponent {
[...]
@Output() onClick = new EventEmitter();
[...]
}
myngbutton.component.html:
<button [...] (click)="onClick.emit()">[...]</button>
app.component.html:
<myngbutton [disabled]="disabled1" (onClick)="alertMsg()">Save 1</myngbutton>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.