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

posted @ 2017-06-25 04:28  judy201654321  阅读(1849)  评论(0编辑  收藏  举报