Angular2學(xué)習(xí)教程之組件中的DOM操作詳解
前言
有時(shí)不得不面對(duì)一些需要在組件中直接操作DOM的情況,如我們的組件中存在大量的CheckBox,我們想獲取到被選中的CheckBox,然而這些CheckBox是通過(guò)循環(huán)產(chǎn)生的,我們無(wú)法給每一個(gè)CheckBox指定一個(gè)ID,這個(gè)時(shí)候可以通過(guò)操作DOM來(lái)實(shí)現(xiàn)。angular API中包含有viewChild,contentChild等修飾符,這些修飾符可以返回模板中的DOM元素。
指令中的DOM操作
@Directive({
selector: 'p'
})
export class TodoDirective{
constructor(el: ElementRef, renderer: Renderer){
renderer.setElementStyle(el.nativeElement, 'backgroundColor', 'red');
}
}
以上聲明了一個(gè)指令,使用是需要在module中的declarations中聲明。該指令的作用是將p元素的backgroundColor設(shè)置為red。
-ElementRef是一個(gè)允許直接獲取DOM元素的一個(gè)類,該類包含一個(gè)nativeElement屬性。當(dāng)不允許直接操作原生DOM元素時(shí),該屬性值為null。
-Renderer該類包含大量可以用來(lái)操作DOM原生的方法。
@ViewChild和@ViewChildren
每一個(gè)組件都有一個(gè)視圖模板,通過(guò) template或templateUrl引入。想要獲取視圖模板中的DOM元素則可以使用@ViewChild和@ViewChildren修飾符。他們可以接受模板變量或元素標(biāo)簽或模板類名來(lái)獲取DOM節(jié)點(diǎn)。@ViewChild返回ElementRef類引用(獲取組件時(shí)則直接使用組件類名),而@ViewChildren返回QueryList<ElementRef> 。
//模板內(nèi)容
<p *ngFor='let item of todos' #name>{{ item.name }}</p>
//組件中獲取DOM
@ViewChildren('name')
todoNames: QueryList<ElementRef>;
@ViewChild('name')
todoName: ElementRef;
ngAfterViewInit(){
this.todoNames.forEach(e=>console.log(e.nativeElement.innerText));
console.log(this.todoName.nativeElement.innerText);
}
@ViewChild('name')和@ViewChildren('name')通過(guò)name模板變量獲取p標(biāo)簽DOM節(jié)點(diǎn),可以在ngAfterViewInit聲明周期鉤子中獲取節(jié)點(diǎn)信息,當(dāng)然也可以在其他函數(shù)中,只要保證視圖完成初始化即可。
QueryList是一個(gè)不可變的列表,其存在一個(gè)名為changes的Observable變量,因此可以被訂閱,結(jié)合notifyOnChanges方法,可以實(shí)時(shí)查看QueryList中變量的變化。調(diào)用notifyOnChanges函數(shù)后,當(dāng)組件的輸入發(fā)生變化時(shí)會(huì)觸發(fā)Observable發(fā)出新的值,這樣當(dāng)todoNames: QueryList<ElementRef>有更新時(shí),便能通過(guò)下面代碼查看到變化:
this.todoNames.changes.subscribe(data => data._results.forEach( e=>console.log(e.nativeElement.innerText))); this.todoNames.notifyOnChanges();
@ContentChild和@ContentChildren
看著與@ViewChild和@ViewChildren很相似,但@ContentChild和@ContentChildren是獲取組件標(biāo)簽中的內(nèi)容的,懶得寫(xiě)例子,這里直接貼上angular中文官網(wǎng)的一個(gè)例子:
import {Component, ContentChildren, Directive, Input, QueryList} from '@angular/core';
@Directive({selector: 'pane'})
export class Pane {
@Input() id: string;
}
@Component({
selector: 'tab',
template: `
<div>panes: {{serializedPanes}}</div>
`
})
export class Tab {
@ContentChildren(Pane) panes: QueryList<Pane>;
get serializedPanes(): string { return this.panes ? this.panes.map(p => p.id).join(', ') : ''; }
}
@Component({
selector: 'example-app',
template: `
<tab>
<pane id="1"></pane>
<pane id="2"></pane>
<pane id="3" *ngIf="shouldShow"></pane>
</tab>
<button (click)="show()">Show 3</button>
`,
})
export class ContentChildrenComp {
shouldShow = false;
show() { this.shouldShow = true; }
}
可以看出@ContentChildren(Pane) panes: QueryList<Pane>;獲取的是組件Tab中的內(nèi)容:
<tab> <pane id="1"></pane> <pane id="2"></pane> <pane id="3" *ngIf="shouldShow"></pane> </tab>
與@ViewChild類似@ContentChild獲取的是第一個(gè)Pane指令,獲取DOM元素后,可以采用類似的方式處理。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家腳本之家的支持。
- 詳解Angular 4.x 動(dòng)態(tài)創(chuàng)建組件
- Angular 2父子組件數(shù)據(jù)傳遞之@ViewChild獲取子組件詳解
- angular中不同的組件間傳值與通信的方法
- Angular父組件調(diào)用子組件的方法
- Angular5給組件本身的標(biāo)簽添加樣式class的方法
- Angular 2父子組件數(shù)據(jù)傳遞之@Input和@Output詳解(下)
- Angular入口組件(entry component)與聲明式組件的區(qū)別詳解
- angular2倒計(jì)時(shí)組件使用詳解
- 詳解angular2封裝material2對(duì)話框組件
- 簡(jiǎn)單談?wù)凙ngular中的獨(dú)立組件的使用
相關(guān)文章
如何解決手機(jī)瀏覽器頁(yè)面點(diǎn)擊不跳轉(zhuǎn)瀏覽器雙擊放大網(wǎng)頁(yè)
這篇文章主要介紹了如何解決手機(jī)瀏覽器頁(yè)面點(diǎn)擊不跳轉(zhuǎn)瀏覽器雙擊放大網(wǎng)頁(yè)的相關(guān)資料,需要的朋友可以參考下2016-07-07
Angularjs單選改為多選的開(kāi)發(fā)過(guò)程及問(wèn)題解析
在項(xiàng)目中遇到這樣的需求想把下拉框的單選改為多選,怎么實(shí)現(xiàn)呢?下面小編通過(guò)本文給大家分享angularjs單選改為多選的開(kāi)發(fā)過(guò)程及問(wèn)題解析,需要的朋友參考下2017-02-02
解決Angular.js中使用Swiper插件不能滑動(dòng)的問(wèn)題
下面小編就為大家分享一篇解決Angular.js中使用Swiper插件不能滑動(dòng)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
詳細(xì)談?wù)凙ngularJS的子級(jí)作用域問(wèn)題
大家在使用angularjs的時(shí)候,很容易忽略AngularJS自帶指令的作用域問(wèn)題,有一些指令會(huì)產(chǎn)生獨(dú)立的自己作用域,造成子級(jí)無(wú)法與父級(jí)作用域雙向綁定的問(wèn)題。下面我們來(lái)看看這些問(wèn)題,有需要的可以參考借鑒。2016-09-09
對(duì)angularJs中$sce服務(wù)安全顯示html文本的實(shí)例
今天小編就為大家分享一篇對(duì)angularJs中$sce服務(wù)安全顯示html文本的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09

