elementRef


elementRef.nativeElementk可以得到一個元素的引用,通過這個引用可以隨意的操作dom樣式

import {Component, ElementRef, OnInit} from '@angular/core';

@Component({
  selector: 'app-example1',
  templateUrl: './example1.component.html',
  styleUrls: ['./example1.component.css']
})
export class Example1Component implements OnInit {

  constructor(private el:ElementRef) {

  }
  show(){
    this.el.nativeElement.style.color="red";
    console.log(this.el.nativeElement)
  }
  ngOnInit() {
  }

}

獲取元素的引用還有一種方法,通過吧$event傳入事件中,就可以與拿到這個事件的所有屬性,只要元素有事發生,就可以拿到元素的所有屬性,換句話說,可以重寫這個元素的樣式

e.target就是這個元素的引用


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM