最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解Angular如何正確的操作DOM

 更新時間:2018年07月06日 15:05:01   作者:Shapeying  
這篇文章主要介紹了詳解Angular如何正確的操作DOM,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

無奈接手了一個舊項(xiàng)目,上一個老哥在Angular項(xiàng)目中大量使用了JQuery來操作DOM,真的是太不講究了。那么如何優(yōu)雅的使用Angular的方式來操作DOM呢?

獲取元素

1、ElementRef  ---   A wrapper around a native element inside of a View.

在組件的 constructor中注入ElementRef,可以獲取到整個組件元素的包裹。

@Component({
 selector: 'app-test-page',
 templateUrl: './test-page.component.html',
 styleUrls: ['./test-page.component.scss']
})
export class TestPageComponent implements OnInit {

 constructor(
 private el: ElementRef
 ) { }

 ngOnInit() {
 }

 getDomTest() {
 console.dir(this.el);
 }
}

ElementRef中的nativeElement即是組件最外層的DOM元素。再通過原生的DOM定位方式,即可獲取到指定的selector元素。

getDomTest() {
 console.dir(this.el.nativeElement.querySelector('.test-get-dom')); // 獲取指定的子元素
 }

2、@viewChild()  ---    You can use ViewChild to get the first element or the directive matching the selector from the  view DOM.

@viewChild可以獲取指定的元素, 指定的方式可以是本地變量或者組件類型;

// HTML
<div class="tip-test-wrapper">
   // 本地變量綁定button按鈕
 <button class="test-get-dom" #testdom (click)="getDomTest()">測試獲取DOM</button>
 </div>
  // Dialog組件
<app-dialog></app-dialog>


// ts
import { DialogComponent } from './../../common/components/dialog/dialog.component';


@Component({
 selector: 'app-test-page',
 templateUrl: './test-page.component.html',
 styleUrls: ['./test-page.component.scss']
})
export class TestPageComponent implements OnInit {
  // 通過本地變量獲取元素 可通過read來指定獲取的元素類型
 @ViewChild('testdom' , { read: ViewContainerRef }) viewcontainer: ViewContainerRef;
 @ViewChild('testdom') viewelement: ElementRef;

  // 通過組件類型來獲取
 @ViewChild(DialogComponent) viewcontent: DialogComponent;

 constructor(
 private el: ElementRef
 ) { }

 ngOnInit() {
 }

 getDomTest() {
 // console.dir(this.el.nativeElement.querySelector('.test-get-dom'));
 console.dir(this.viewcontainer);
 console.dir(this.viewelement);
 console.dir(this.viewcontent);
 }
}

備注:ElementRef或者 @viewChild 獲取元素,一定要在 ngAfterViewInit 周期之后再使用。

3、@viewChildren --   You can use ViewChildren to get the {@link QueryList} of elements or directives from theview DOM.

@viewChild會返回符合條件的第一個元素,如果需要獲取多個符合條件的元素呢? @viewChildren會返回所有符合條件的元素的list。 指定selector的方式與@viewChild一致。

// 復(fù)制一個元素
 <div class="tip-test-wrapper">
 <button class="test-get-dom" #testdom (click)="getDomTest()">測試獲取DOM</button>
 </div>
 <div class="tip-test-wrapper">
 <button class="test-get-dom" #testdom (click)="getDomTest()">測試獲取DOM</button>
 </div>
</div>
<app-dialog></app-dialog>
<app-dialog></app-dialog>

// ts
import { DialogComponent } from './../../common/components/dialog/dialog.component';


@Component({
 selector: 'app-test-page',
 templateUrl: './test-page.component.html',
 styleUrls: ['./test-page.component.scss']
})
export class TestPageComponent implements OnInit {

 @ViewChild('testdom' , { read: ViewContainerRef }) viewcontainer: ViewContainerRef;
 @ViewChild('testdom') viewelement: ElementRef;
 @ViewChildren('testdom') viewelements: QueryList<any>;
 @ViewChild(DialogComponent) viewcontent: DialogComponent;
 @ViewChildren(DialogComponent) viewcontents: QueryList<DialogComponent>;

 constructor(
 private el: ElementRef
 ) { }

 ngOnInit() {
 }

 getDomTest() {
 // console.dir(this.el.nativeElement.querySelector('.test-get-dom'));
 // console.dir(this.viewcontainer);
 console.dir(this.viewelement);
 console.dir(this.viewelements);
 console.dir(this.viewcontent);
 console.dir(this.viewcontents);
 }

操作DOM  --- Renderer2

在獲取dom之后,如何對dom進(jìn)行操作呢?原生的domAPI是一種選擇,但是Angular提供了更好的跨平臺方式   Renderer2。

引入 Renderer2  , 然后在construct中注入。

import { Component, OnInit , ViewContainerRef , ElementRef , ViewChild, Renderer2 , ViewChildren, QueryList} from '@angular/core';

import { DialogComponent } from './../../common/components/dialog/dialog.component';


@Component({
 selector: 'app-test-page',
 templateUrl: './test-page.component.html',
 styleUrls: ['./test-page.component.scss']
})
export class TestPageComponent implements OnInit {

 @ViewChild('testdom' , { read: ViewContainerRef }) viewcontainer: ViewContainerRef;
 @ViewChild('testdom') viewelement: ElementRef;
 @ViewChildren('testdom') viewelements: QueryList<any>;
 @ViewChild(DialogComponent) viewcontent: DialogComponent;
 @ViewChildren(DialogComponent) viewcontents: QueryList<DialogComponent>;

 constructor(
 private render: Renderer2,
 private el: ElementRef
 ) { }

 ngOnInit() {
 }

 getDomTest() {
 // 修改元素顏色
 this.render.setStyle(this.viewelement.nativeElement , 'color' , 'red');
 }
}

renderer2提供了豐富的API供使用,如下:

總結(jié)

通過elementRef或者@viewChild @viewChildren獲取元素,再通過renderer2提供的API來操作元素。不過記得在不要在 ngAfterViewInit 周期之前使用。通過Angular提供的方式,可以滿足大部分的操作DOM的需求了。如果有特殊的場景,當(dāng)然還是原生DOM擼起來呀

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 利用Angularjs中模塊ui-route管理狀態(tài)的方法

    利用Angularjs中模塊ui-route管理狀態(tài)的方法

    這篇文章主要給大家介紹了如何用Angularjs中的模板ui-route來管理狀態(tài)的方法,文中通過示例代碼介紹的很詳細(xì),相信對大家的理解和學(xué)習(xí)具有一定的參考借鑒價值,有需要的朋友可以一起來學(xué)習(xí)學(xué)習(xí)。
    2016-12-12
  • monaco?editor在Angular的使用詳解

    monaco?editor在Angular的使用詳解

    這篇文章主要為大家介紹了monaco?editor在Angular的使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • 淺談Angular中ngModel的$render

    淺談Angular中ngModel的$render

    下面小編就為大家?guī)硪黄獪\談Angular中ngModel的$render。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-10-10
  • AngularJS 應(yīng)用身份認(rèn)證的技巧總結(jié)

    AngularJS 應(yīng)用身份認(rèn)證的技巧總結(jié)

    這篇文章主要介紹了AngularJS 應(yīng)用身份認(rèn)證的技巧總結(jié),具有一定的參考價值,有需要的可以了解一下。
    2016-11-11
  • AngularJS 限定$scope的范圍實(shí)例詳解

    AngularJS 限定$scope的范圍實(shí)例詳解

    這篇文章主要介紹了AngularJS 限定$scope的范圍實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • angularJs在多個控制器中共享服務(wù)數(shù)據(jù)的方法

    angularJs在多個控制器中共享服務(wù)數(shù)據(jù)的方法

    今天小編就為大家分享一篇angularJs在多個控制器中共享服務(wù)數(shù)據(jù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • AngularJs定制樣式插入到ueditor中的問題小結(jié)

    AngularJs定制樣式插入到ueditor中的問題小結(jié)

    這篇文章主要介紹了AngularJs定制樣式插入到ueditor中的問題小結(jié)的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-08-08
  • 分享使用AngularJS創(chuàng)建應(yīng)用的5個框架

    分享使用AngularJS創(chuàng)建應(yīng)用的5個框架

    如果你計(jì)劃使用AngularJS創(chuàng)建你的Web應(yīng)用,那現(xiàn)在就開始吧。你不需要有任何的恐懼和擔(dān)心,因?yàn)楝F(xiàn)在有很多的框架都可以很好地支持AngularJS
    2015-12-12
  • AngularJS通過$sce輸出html的方法

    AngularJS通過$sce輸出html的方法

    不知道大家有沒有發(fā)現(xiàn)在用AngularJS作為前端搭建個人博客的時候,發(fā)現(xiàn)用AngularJs輸出html的時候,瀏覽器并不解析這些html標(biāo)簽,這里我們需要其顯示angular輸出的html能被瀏覽器解析怎么辦呢?不知道Angularjs如何實(shí)現(xiàn)這種功能的通過這篇文章來看看吧。
    2016-09-09
  • Angular應(yīng)用tsconfig.json中的lib屬性示例解析

    Angular應(yīng)用tsconfig.json中的lib屬性示例解析

    這篇文章主要介紹了Angular應(yīng)用tsconfig.json中的lib屬性示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07

最新評論

湄潭县| 漾濞| 和平区| 乐业县| 上栗县| 来宾市| 澜沧| 花莲县| 彩票| 于都县| 宁晋县| 措勤县| 黄龙县| 江永县| 报价| 武安市| 潜山县| 沧州市| 曲水县| 搜索| 定州市| 兴业县| 祁门县| 林芝县| 老河口市| 江阴市| 息烽县| 炎陵县| 南澳县| 新安县| 雅安市| 报价| 伊吾县| 英吉沙县| 河北省| 宣威市| 法库县| 辽宁省| 绥阳县| 盐山县| 固阳县|