Angular 2/4 set focus on input element

后端 未结 4 1113
渐次进展
渐次进展 2021-01-07 20:48

How can I set focus on an input by (click) event? I have this function in place but I\'m clearly missing something (angular newbie here)

sTbState: string = \         


        
4条回答
  •  长发绾君心
    2021-01-07 21:22

    Get input element as native elements in ts file.

    //HTML CODE
    
    
    
    
    //TS CODE
    @ViewChild("focusTrg") trgFocusEl: ElementRef;
    
      onSetFocus() {
         setTimeout(()=>{
          this.trgFocusEl.nativeElement.focus();
        },100);
      }
    

    we need to put this.trgFocusEl.nativeElement.focus(); in setTimeout() then it'll work fine otherwise it will throw undefined error.

提交回复
热议问题