Angular 2 get host element reference

匿名 (未验证) 提交于 2019-12-03 02:56:01

问题:

How can I get the host element reference in angular 2? In my case I want to know that in my component if it has a focus or not. Is it possible?

Best Regards!

回答1:

You get the host element reference using

class MyComponent {   constructor(private elRef:ElementRef) {     console.log(this.elRef.nativeElement);   } } 

You can also subscribe to the focus event

class MyComponent {   @HostBinding() tabindex = 0;   @HostListener('focus', ['$event'])   onFocus(event) {     console.log(event);   } } 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!