简体   繁体   English

使用Renderer2 / HostListener进行单击的外部事件的Angular 4

[英]Angular 4 using Renderer2/HostListener for clicked outside event

I have developed a component consisting of basic input field and a suggestions dropdown list which is shown when user clicks into input (similar to ng-select ). 我开发了一个由基本输入字段和建议下拉列表组成的组件,当用户单击输入时会显示该列表(类似于ng-select )。 However, when user wants to close dropdown list can either unfocus it with tab/shift-tab/esc/... or it can click somewhere outside the whole component. 但是,当用户要关闭下拉列表时,可以使用tab / shift-tab / esc / ...使其失去焦点,或者可以单击整个组件外部的某个位置。 When developing for browser platform in Angular 4, this is quite simple with HostListener, ElementRef and nativeElement. 在Angular 4中为浏览器平台开发时,使用HostListener,ElementRef和nativeElement非常简单。 My current solution for browser platform is (also seen in other SO question ): 我当前的浏览器平台解决方案是(也在其他SO问题中看到):

@HostListener('document:click', ['$event'])
onClickedOutside(event: any): void {
  if (!this.elementRef.nativeElement.contains(event.target)) {
    this.hideDropdown();
  }
}

Since Angular aim is to be platform indpendent, I want to standardize this component to be compatible with webworker platform (along browser platform), because I'd like to use it in my project (idea is to transfer some calculation logic to client-side, to minimize workload on server and still have responsive UI). 由于Angular的目标是成为平台的独立者,因此我想将该组件标准化以与Webworker平台(以及浏览器平台)兼容,因为我想在项目中使用它(想法是将一些计算逻辑转移到客户端,以最大程度地减少服务器上的工作量,并且仍然具有响应式UI。 I already tried to replace ... nativeElement.contains ... with some other method which would perform check, but unfortunately when this event is being triggered under webworker platform, $event has only some basic info about mouse position, without actual target which is clicked (I think properties of DomRenderer class are shown). 我已经尝试用其他将执行检查的方法替换... nativeElement.contains ... ,但是不幸的是,在Webworker平台下触发此事件时, $event仅具有有关鼠标位置的一些基本信息,而没有实际目标单击(我认为显示了DomRenderer类的属性)。

I'm not really well informed with the current state of webworker platform and if it's ready for such things, so I'm looking for the solution or workaround on how to track outside click. 我对Webworker平台的当前状态以及它是否已经准备好了解这些状态并不太了解,所以我正在寻找有关如何跟踪外部点击的解决方案或解决方法。 If this is somehow not possible, I would please you to at least describe why my idea cannot be implemented or how would you do it (for example stay on browser platform and use webworker platform when this will be supported). 如果无法通过某种方式实现,那么请您至少描述一下为什么我的想法无法实现或您将如何实现(例如,当支持时,请停留在浏览器平台上并使用webworker平台)。 Thank you in advance! 先感谢您!

Angular 4 provides a constant DOCUMENT in @angular/platform-browser package which you can use with injection in the constructor of your component Angular 4在@ angular / platform-b​​rowser包中提供了一个常量文档,您可以在组件的构造函数中与注入一起使用

import { Inject } from "@angular/core";
import { DOCUMENT } from "@angular/platform-browser";

export class DumpComponent implements OnInit {
 constructor(@Inject(DOCUMENT) private document: Document) { }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM