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就是這個元素的引用