angular获取dom节点


angular获取dom节点

.html文件中

<div #myBox>
  dom节点
</div>

在.ts文件中
引入 ViewChild

import { ViewChild } from '@angular/core';
export class NewsComponent implements OnInit {
//写在类里面
  @ViewChild("myBox") myBox: any
  //ngAfterViewInit生命周期函数里面获取dom
  ngAfterViewInit(): void {
    console.log(this.myBox.nativeElement.innerHTML)
    this.myBox.nativeElement.style.width = "100px";
    this.myBox.nativeElement.style.height = "100px";
    this.myBox.nativeElement.style.background = "red";
  }

}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM