2

I have component in component in component. So in the root component i want to focus input in the deepest child when i am clicking somewhere in .grandparent-container.

As you can see in the example i could have multiple components on the page. So i need to focus only child input of the clicked component. Here is example.

I tried to create a [focus] directive, but it works only if i have one grandparent on the page, because directive can't to figure out what exactly input it should focus.

Select element by id is not the case also.

How can i achieve this?

1 Answer 1

2

You could build a simple focus messaging service using an Observable Subject. Clicked controller tells the service it has been clicked. Child controller subscribes and sets focus. Good use case for a component-scoped provider.

Here's a fork of your example. https://stackblitz.com/edit/angular-1nbbk8

Sign up to request clarification or add additional context in comments.

3 Comments

Just replaced the link with a slightly better version.
Great. Thank you a lot!
Excellent solution, I've implemented it on my app. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.