How to set focus on element with binding?

后端 未结 9 959
南方客
南方客 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:44

    The best way of setting focus on an element with angular2 is by using the Renderer and invoke an method on the element. There is no way of doing this without elementRef.

    This results in something like this:

    this.renderer.invokeElementMethod(this.input.nativeElement, 'focus', []);
    

    Where renderer gets injected in the constructor using protected renderer : Renderer

提交回复
热议问题