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