I need to be able to switch focus to an input element when some event occurs. How do I do that in Angular 2?
For example:
<input (keyUp)="processKeyUp($event)"/>
<input (focusme)="alert('i am focused')"/>
I want to focus the 2nd input box when a certain key is pressed in the first. I think I need to use a custom event (focusme
in the snippet), but I don't know where or how to declare it, and whether to use a @Directive
annotation for it, or include its definition in a component somehow. In short, I am stumped.
UPDATE
Forgot to mention, I know I can do that by using local variables in the html, but I want to be able to do it from the component, and I want to be able to do complex logic when firing the focusme
event so that controls listening to it can determine if it is meant for them or not.
Thanks!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…