詳解如何在Angular中快速定位DOM元素
在使用Angular2+中,經(jīng)常會想快速的去選擇DOM上的某個元素,如果是剛上手Angular,有可能直接就使用原生DOM操作或者導(dǎo)入jQuery再進行DOM操作,既然都使用了Angular了,有沒有更好的方法呢?答案是肯定的。
通過ElementRef
先上代碼:
import {Component, ElementRef, OnInit} from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
color:string;
title = 'button !';
constructor(private el:ElementRef){}
setHeight(){
this.el.nativeElement.querySelector('.btn1').style.height = '300px';
}
ngOnInit(){
this.setHeight();
}
}
<h1>
{{title}}
</h1>
<div>
<button myHighlight class="btn btn1">按鈕一</button>
<button myHighlight class="btn">按鈕二</button>
<button myHighlight class="btn">按鈕三</button>
</div>
效果是這樣:

上述代碼中的nativeElement其實包含的是組件中所有的DOM元素,如下圖所示:

通過調(diào)用querySelectorAPI就能獲取頁面元素,需要注意的是querySelector只返回第一個元素,當你需要選擇多個元素的時候可以使用querySelectorAll。
通過@viewChild
<h1>
{{title}}
</h1>
<div>
<button myHighlight class="btn btn1">按鈕一</button>
<button myHighlight class="btn" #btn>按鈕二</button> <!--增加一個變量-->
<button myHighlight class="btn">按鈕三</button>
</div>
import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
@ViewChild('btn') btn:ElementRef;//通過@ViewChild獲取元素
color:string;
title = 'button !';
constructor(private el:ElementRef){}
setHeight(){
this.el.nativeElement.querySelector('.btn1').style.height = '300px';
}
setWidth(){
this.btn.nativeElement.style.width = '200px';//定義寬度
}
ngOnInit(){
this.setHeight();
this.setWidth();
}
}
效果如下:

如果多個HTML元素都定義了相同的變量,使用@viewChild時只能選擇到第一個元素。
更好的方法是配合renderer2對象提供的API去實現(xiàn)同樣的效果,這樣減少應(yīng)用層與渲染層之間強耦合關(guān)系:
import {Component, ElementRef, OnInit, Renderer2, ViewChild} from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
@ViewChild('btn') btn:ElementRef;
color:string;
title = 'button !';
//初始化renderer2
constructor(private el:ElementRef,private renderer2: Renderer2){}
setHeight(){
this.el.nativeElement.querySelector('.btn1').style.height = '300px';
}
setWidth(){
// this.btn.nativeElement.style.width = '200px';
//使用renderer2的setStyle方法設(shè)置寬度
this.renderer2.setStyle(this.btn.nativeElement,'width','200px')
}
ngOnInit(){
this.setHeight();
this.setWidth();
}
}
參考文章中都提到了@viewChild配合renderer選擇元素,但是在Angular4中renderer已經(jīng)廢棄掉了,變成了renderer2。
renderer2API中還有其他的一些方法可以用來進行一些DOM操作:
class Renderer2 {
data : {[key: string]: any}
destroy() : void
createElement(name: string, namespace?: string) : any
createComment(value: string) : any
createText(value: string) : any
destroyNode : (node: any) => void |
appendChild(parent: any, newChild: any) : void
insertBefore(parent: any, newChild: any, refChild: any) : void
removeChild(parent: any, oldChild: any) : void
selectRootElement(selectorOrNode: string|any) : any
parentNode(node: any) : any
nextSibling(node: any) : any
setAttribute(el: any, name: string, value: string, namespace?: string) : void
removeAttribute(el: any, name: string, namespace?: string) : void
addClass(el: any, name: string) : void
removeClass(el: any, name: string) : void
setStyle(el: any, style: string, value: any, flags?: RendererStyleFlags2) : void
removeStyle(el: any, style: string, flags?: RendererStyleFlags2) : void
setProperty(el: any, name: string, value: any) : void
setValue(node: any, value: string) : void
listen(target: 'window'|'document'|'body'|any, eventName: string, callback: (event: any) => boolean | void) : () => void
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
angularJs中json數(shù)據(jù)轉(zhuǎn)換與本地存儲的實例
今天小編就為大家分享一篇angularJs中json數(shù)據(jù)轉(zhuǎn)換與本地存儲的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10
AngularJS對動態(tài)增加的DOM實現(xiàn)ng-keyup事件示例
這篇文章主要介紹了AngularJS對動態(tài)增加的DOM實現(xiàn)ng-keyup事件示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03
使用AngularJS對表單提交內(nèi)容進行驗證的操作方法
AngularJS是一款優(yōu)秀的前端JS框架,已經(jīng)被用于Google的多款產(chǎn)品當中。下面通過本文給大家分享使用AngularJS對表單提交內(nèi)容進行驗證的操作方法,需要的的朋友參考下吧2017-07-07
angularjs實現(xiàn)對表單輸入改變的監(jiān)控(ng-change和watch兩種方式)
這篇文章主要介紹了angularjs通過ng-change和watch兩種方式實現(xiàn)對表單輸入改變的監(jiān)控,需要的朋友可以參考下2018-08-08

