How to set focus on element with binding?

后端 未结 9 977
南方客
南方客 2020-12-09 10:23

In Angular2 how can I set binding on element focus. I don\'t want to set it with elementRef. I think in AngularJS there is ngFocus directive In Angular2 there is no such dir

9条回答
  •  悲&欢浪女
    2020-12-09 10:40

    import { Directive, ElementRef, AfterViewChecked } from '@angular/core';
    
    @Directive({
     selector: '[autoFocus]',
    })
    export class FocusDirective implements AfterViewChecked {
    
      constructor(private _elementRef: ElementRef) { }
    
      ngAfterViewChecked() {
        this._elementRef.nativeElement.focus()
      }
    
    }
    

提交回复
热议问题