How can I set focus to another input?

后端 未结 4 2026
天涯浪人
天涯浪人 2020-12-08 04:55

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:



        
4条回答
  •  鱼传尺愫
    2020-12-08 05:17

    You can do this just passing the second input as a variable to the first one

    For example

    HTML

    
    
    
    
     
    

    Later in your js/ts

    @Component({
      selector: 'plunker-app'
    })
    @View({
      templateUrl: 'main.html'
    })
    class PlunkerApp {
    
      constructor() {
      }
    
      processKeyUp(e, el) {
        if(e.keyCode == 65) { // press A
          el.focus();
        }
      }
    }
    

    el is the raw element, so you can use pure javascript on it.

    Here's a plnkr so you can see it working.

    I hope it helps.

提交回复
热议问题