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就是这个元素的引用

You can change the world with your heart,even a lot of changes sometimes unless you won't geiv up....
原文地址:https://www.cnblogs.com/xiongwei2017/p/7075931.html