How to use Angular4 to set focus by element id

后端 未结 6 1450
無奈伤痛
無奈伤痛 2020-12-14 14:11

I am new to Angular, and am trying to use it to set focus on an input with the id \"input1\". I am using the following code:



        
6条回答
  •  失恋的感觉
    2020-12-14 15:05

    component

    import { Component, ElementRef, ViewChild, AfterViewInit} from '@angular/core';
    ... 
    
    @ViewChild('input1', {static: false}) inputEl:ElementRef;
    
    ngAfterViewInit() {
          setTimeout(() => this.inputEl.nativeElement.focus());
    }
    

    html

    
    

提交回复
热议问题