国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

詳解如何在Angular中快速定位DOM元素

2019-11-19 16:34:25
字體:
供稿:網(wǎng)友

在使用Angular2+中,經(jīng)常會(huì)想快速的去選擇DOM上的某個(gè)元素,如果是剛上手Angular,有可能直接就使用原生DOM操作或者導(dǎo)入jQuery再進(jìn)行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其實(shí)包含的是組件中所有的DOM元素,如下圖所示:

通過調(diào)用querySelectorAPI就能獲取頁面元素,需要注意的是querySelector只返回第一個(gè)元素,當(dāng)你需要選擇多個(gè)元素的時(shí)候可以使用querySelectorAll

通過@viewChild

<h1> {{title}}</h1><div><button myHighlight class="btn btn1">按鈕一</button><button myHighlight class="btn" #btn>按鈕二</button> <!--增加一個(gè)變量--><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(); }}

效果如下:

如果多個(gè)HTML元素都定義了相同的變量,使用@viewChild時(shí)只能選擇到第一個(gè)元素。

更好的方法是配合renderer2對象提供的API去實(shí)現(xiàn)同樣的效果,這樣減少應(yīng)用層與渲染層之間強(qiá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選擇元素,但是在Angular4renderer已經(jīng)廢棄掉了,變成了renderer2

renderer2API中還有其他的一些方法可以用來進(jìn)行一些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í)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 鄂伦春自治旗| 漳平市| 集贤县| 屯昌县| 台东县| 佛山市| 绥芬河市| 本溪市| 石门县| 台中县| 无为县| 道孚县| 吉林省| 海伦市| 宜宾市| 都安| 安化县| 宜阳县| 辽阳县| 洛浦县| 城口县| 涡阳县| 望江县| 宁海县| 东丰县| 大渡口区| 青海省| 曲阜市| 吐鲁番市| 长葛市| 崇州市| 文化| 广南县| 曲松县| 连南| 富川| 哈尔滨市| 桂阳县| 闻喜县| 临清市| 盐津县|