Angular 2获取宿主元素参考 [英] Angular 2 get host element reference

查看:56
本文介绍了Angular 2获取宿主元素参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取角度2中的宿主元素引用?就我而言,我想知道它在组件中是否具有焦点.有可能吗?

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?

最诚挚的问候!

推荐答案

您可以使用

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

您还可以订阅focus事件

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

这篇关于Angular 2获取宿主元素参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆