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