Focus an <input> element

前端 未结 4 680
挽巷
挽巷 2020-12-19 01:35

I\'ve many input boxes in a div and I need to focus one of them programmatically.

How to do it?

It\'s something like:



        
4条回答
  •  旧时难觅i
    2020-12-19 01:54

    Take a look at the ViewChild (and ViewChildren like Gunter suggests) annotations.

    You can do something like this:

    @Component({
      selector: 'samples',
      template: `
        
    ` }) export class SamplesComponent { @ViewChild('input3') input3:ElementRef; constructor(private _renderer : Renderer) {} public selectSample() { //as per Eric's suggestion this._renderer.invokeElementMethod(this.input3.nativeElement, 'focus', []); } }

提交回复
热议问题