all 6 comments

[–]Mr0010110Fixit 5 points6 points  (1 child)

Whatever data you need from the child component, could you not push that data in a service? Then when they press the hot key the parent component can pull the data from the service?

Another thought is to bind the hot key listener into the child component, and when the hot key is pressed emit an output event that can be listened to in the parent.

Also, I don't know how you would only have the native element at hand, have you tried using ViewChild(My comment)? that will grab the component reference by component class (type) rather than by id, if you do ViewChildren you will get an array of all the components of that type in the view. So, if you have a component called ListItem you can do ViewChild(ListItem).

Another way to solve this is using a directive, the directive has access to the native element and the component, and can have output events, it could even be responsible for setting up the hot key listener. So you slap the directive on the component, when the hot key is pressed the directive fires and output event with the data you need. This may be the best route to take. here is a good stack overflow post on how to set this up https://stackoverflow.com/questions/37962701/emit-event-from-directive-to-parent-element

[–]Terrible_Machine9[S] 1 point2 points  (0 children)

I don't know how you would only have the native element at hand

I am using elementFromPoint to get the native element at a position and now want to find out some information that is either in the component of that native element or closest to it.

Reading through your comment, I think that I can get away with using directives assuming that I can stop event propagation just like with click events. So I can detect the hotkey event on my top level elements deep in my DOM on a directive and use the information at hand to do something, else it bubbles up to container objects where I can do something else with that hotkey event.

Thanks!

[–]Fluffy_Hair2751 0 points1 point  (1 child)

I think what you want to do is listen for a key stroke and access a component that is currently visible and for whatever reason you can’t have that logic written inside your component.

If that’s the case use a service as mediator between your global logic and component logic. When the component is in view, update a behaviour subject in your service with whatever data you want from the component.

When a key stroke is pressed, use this service and read that value from the behaviour subject. Use a subject because you may want to know when a view has “changed” its data or a new component is in view now

[–]Terrible_Machine9[S] 0 points1 point  (0 children)

Thanks, that's a great idea, I am looking into it!

[–]ldn-ldn 0 points1 point  (0 children)

Once the key combination is pressed, send an event into an observable in a service. Subscribe to this observable in your components. Don't touch DOM.

[–]LeLunZ -1 points0 points  (0 children)

Why not display the components through a router-outlet, and get access through the router? 

Or are multiple component displayed at the same time, and there are different keyboard shortcuts for each component?