我有角反應形式的角應用。我想在基于某些條件關閉瀏覽器選項卡/視窗時警告用戶。
我該如何實作?
我嘗試了以下操作,但無論我回傳什么或使用什么條件,它總是顯示警告訊息。我在 component.ts 檔案中有以下內容:
@HostListener('window:unload', ['$event']) handleClose($event: Event) {
$event.returnValue = false;
if (!this.myForm.pristine) {
$event.preventDefault();
return false;
}
else return true;
}
以及 component.html 檔案中的以下內容
<div (window:beforeunload)="handleClose($event)"></div>
我想要做的是檢查顯示/不顯示彈出警告的條件。非常感激。
uj5u.com熱心網友回復:
嘗試這個
@HostListener('window:beforeunload', ['$event'])
handleClose($event): void {
if (!this.myForm.pristine) { // prevent unload event action.
$event.returnValue = true;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/535090.html
標籤:有角度的角度材料
上一篇:在沒有NgbModule的情況下通過javascript在角度(.ts)中使用boostrap模態?
下一篇:禁用此特定表單的按鈕
