【发布时间】:2025-11-22 19:55:02
【问题描述】:
在我的角度指令中,单击主机元素时会弹出显示。当弹出窗口打开状态时,如果用户在任何地方点击外部,我需要关闭弹出窗口。为此,我尝试使用以下代码。但不起作用。 this.removeComponent(); 方法总是在调用。
@HostListener("window:mouseup", ["$event"]) clickedOut(event) {
if (event.target.nativeElement === this.hostElement) {
//when click on host element do nothing!
return;
}
//when click outside otherthan this.hostElement remove the componenet
this.removeComponent();
}
有人请帮我处理这种情况吗?
【问题讨论】:
-
您能否在 stackbiz 或其他地方重现该场景,我们可以在那里编辑代码,因为这里只有 HostListener 已发布
-
@Sanira - 这是演示stackblitz.com/edit/angular-ivy-fp4hfm?file=src/app/directive/… 点击
Start editing to see some magic happen :)并点击外部删除样式