詳解如何在Angular中快速定位DOM元素
在使用Angular2+中,經常會想快速的去選擇DOM上的某個元素,如果是剛上手Angular,有可能直接就使用原生DOM操作或者導入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元素,如下圖所示:
通過調用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去實現同樣的效果,這樣減少應用層與渲染層之間強耦合關系:
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方法設置寬度 this.renderer2.setStyle(this.btn.nativeElement,'width','200px') } ngOnInit(){ this.setHeight(); this.setWidth(); } }
參考文章中都提到了@viewChild
配合renderer
選擇元素,但是在Angular4
中renderer
已經廢棄掉了,變成了renderer2
。
renderer2
API中還有其他的一些方法可以用來進行一些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 }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
利用Ionic2 + angular4實現一個地區(qū)選擇組件
ionic是一個移動端開發(fā)框架,使用hybird技術,只要使用前端開發(fā)技術就可以開發(fā)出電腦端,安卓端和ios端的站點程序。下面這篇文章主要給大家介紹了關于利用Ionic2 + angular4實現一個地區(qū)選擇組件的相關資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-07-07AngularJS中ng-options實現下拉列表的數據綁定方法
今天小編就為大家分享一篇AngularJS中ng-options實現下拉列表的數據綁定方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08詳解Angular Forms中自定義ngModel綁定值的方式
在Angular應用中有兩種方式來實現表單綁定,但是對于一些特殊的表單控件沒法實現,這篇文章主要介紹了詳解Angular Forms中自定義ngModel綁定值的方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12