简体   繁体   中英

Find the angular2 Component corresponding to a DOM element

I have a use case where a mouseup event handler is attached to document. I would like to get the module which corresponds to the event target element.

One solution is to loop through a list of known (target) component instances and compare the target element with the viewContainerRef.element. Another solution would be to dynamicaly attach a mouseup event listener to all known component instances.

Is there anything cleaner of clever to do? The target components have been created dynamically so there is no template binding and there is no direct relationship between the Component owning the mouseup event handler and the target Components.

I decided to create a service which holds an Observable. Each "target" Component subscribes to the service and decides if it's concerned about the message (which carries the source event) comparing the id of the event target element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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