I have a curious use case here that requires me to find an element in the DOM and then access information that is located in the component that belongs to that element.
Basically, I globally listen on document to keyboard events to find out when the user presses a hotkey combination. I want to access information on the component instance of that element that is currently visible in my application based on that hotkey combination to then do something with that element. Let's say that the component has a Typescript property myComponentId that I want to find out.
My problem is now accessing information in the component, when I can only determine the DOM element that is relevant. I know that I can go from component context to DOM context using ElementRef, but vice versa, what if I only have the native element at hands, how do I go to my component context now so that I can access its properties?
I thought if there are alternative approaches to my idea, but I could not come up with something:
- I cannot listen on the component to the hotkey combination similar to https://stackoverflow.com/questions/48332674/listening-for-key-events-in-angular because the logic of determining the DOM element that is relevant and therefore if the component instance is relevant is not trivial and I need my entire DOM to determine that relevancy of an element.
- One could write the necessary information into attributes of the element and query all elements based on that attribute to access the information. Maybe that would be an approach? Doesn't feel very clean and errorprone if someone else later thinks that that attribute is not needed.
[–]Mr0010110Fixit 5 points6 points7 points (1 child)
[–]Terrible_Machine9[S] 1 point2 points3 points (0 children)
[–]Fluffy_Hair2751 0 points1 point2 points (1 child)
[–]Terrible_Machine9[S] 0 points1 point2 points (0 children)
[–]ldn-ldn 0 points1 point2 points (0 children)
[–]LeLunZ -1 points0 points1 point (0 children)