如何获得主机元素参考? [英] How to get host element reference?

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

问题描述

如何在Angular 2中获取宿主元素引用?

How can I get the host element reference in Angular 2?

就我而言,我想知道我的组件是否具有焦点.

In my case, I want to know if my component has a focus or not.

推荐答案

您可以使用

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);
  }
}

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

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