【问题标题】:can't have access to dom element in angular2无法访问 angular2 中的 dom 元素
【发布时间】:2017-11-25 13:36:09
【问题描述】:

我正在使用Angular-cli。我有:

<div id="show"></div>

我需要在 .ts 文件中访问这个 dom 元素 document.getElementById('show') 之类的东西返回 null

【问题讨论】:

标签: angular typescript


【解决方案1】:

试试这个

<div id="show">

constructor(private elRef:ElementRef) {}

ngAfterViewInit() {
 this.elRef.nativeElement.querySelector('#show');
}

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签