angularDom操作以及@ViewChild、執(zhí)行 css3 動(dòng)畫

DOM操作

ngAfterViewInit(){ 
//原生JS操作
var boxDom:any=document.getElementById('box'); 
boxDom.style.color='red'; 
}
//@ViewCHild操作DOM
import { Component ,ViewChild,ElementRef} from '@angular/core';
@ViewChild('myattr') myattr: ElementRef;
<div #myattr></div>
let attrEl = this.myattr.nativeElement;

父子組件中通過 ViewChild 調(diào)用子組件 的方法

//調(diào)用子組件給子組件定義一個(gè)名稱
<app-footer #footerChild></app-footer>
// 引入 ViewChild
import { Component, OnInit ,ViewChild} from '@angular/core';
// ViewChild 和剛才的子組件關(guān)聯(lián)起來
@ViewChild('footerChild') footer;
//調(diào)用子組件
run(){this.footer.footerRun(); }
//html
<app-header #header></app-header>
<div #myBox>
   我是一個(gè)dom節(jié)點(diǎn)
</div>
<button (click)="getChildRun()">獲取子組件的方法</button>
//js代碼
/*
ViewChild獲取dom節(jié)點(diǎn)
    1、模板中給dom起一個(gè)名字
      <div #myBox>
        我是一個(gè)dom節(jié)點(diǎn)
      </div>
    2、在業(yè)務(wù)邏輯里面引入ViewChild
    import { Component, OnInit,ViewChild} from '@angular/core';
    3、 寫在類里面    @ViewChild('myBox') myBox:any;
    4、ngAfterViewInit生命周期函數(shù)里面獲取dom
    this.myBox.nativeElement
*/
import { Component, OnInit,ViewChild} from '@angular/core';
@Component({
  selector: 'app-news',
  templateUrl: './news.component.html',
  styleUrls: ['./news.component.scss']
})
export class NewsComponent implements OnInit {
  //獲取dom節(jié)點(diǎn)
  @ViewChild('myBox') myBox:any;
  //獲取一個(gè)組件
  @ViewChild('header') header:any;
  constructor() { }
  ngOnInit() {
  }
  ngAfterViewInit(): void {
    console.log(this.myBox.nativeElement);
    this.myBox.nativeElement.style.width='100px';
    this.myBox.nativeElement.style.height='100px';
    this.myBox.nativeElement.style.background='red';
    console.log(this.myBox.nativeElement.innerHTML);
  }
  getChildRun(){
    //調(diào)用子組件里面的方法
     this.header.run()     
  }
}

CSS動(dòng)畫

ngOnInit() {   
       //組件和指令初始化完成   并不是真正的dom加載完成
       let oBox:any=document.getElementById('box');
       console.log(oBox.innerHTML);
       oBox.style.color="red";
       //獲取不到dom節(jié)點(diǎn)
      /*
       let oBox1:any=document.getElementById('box1');
       console.log(oBox1.innerHTML);
       oBox1.style.color="blue";
      */ 
  }
  //視圖加載完成以后觸發(fā)的方法    dom加載完成  (建議把dom操作放在這個(gè)里面)  
  ngAfterViewInit(){
       let oBox1:any=document.getElementById('box1');
       console.log(oBox1.innerHTML);
       oBox1.style.color="blue";
  }
hideAside(){
    //原生js獲取dom節(jié)點(diǎn)
    var asideDom:any=document.getElementById('aside');
    asideDom.style.transform="translate(100%,0)";
 }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • It's a common pattern in React to wrap a component in an ...
    jplyue閱讀 3,408評論 0 2
  • Yan Ruyu_Vue學(xué)習(xí)筆記 Vue基本認(rèn)知 Vue是一個(gè)JavaScript框架,它的特點(diǎn)在于使用者不用再思...
    YuJianChi閱讀 657評論 0 0
  • 作為一個(gè)合格的開發(fā)者,不要只滿足于編寫了可以運(yùn)行的代碼。而要了解代碼背后的工作原理;不要只滿足于自己的程序...
    六個(gè)周閱讀 8,679評論 1 33
  • Angular7入門總結(jié)篇 一、Angular 介紹 根據(jù)項(xiàng)目數(shù)統(tǒng)計(jì) angular(1.x 、2.x 、4.x、...
    痞_4fc8閱讀 381評論 0 3
  • 前端開發(fā)面試題 面試題目: 根據(jù)你的等級和職位的變化,入門級到專家級,廣度和深度都會(huì)有所增加。 題目類型: 理論知...
    怡寶丶閱讀 2,687評論 0 7

友情鏈接更多精彩內(nèi)容