Angular獲取ngIf渲染的Dom元素示例
Angular獲取普通Dom元素的方法
通過(guò)模板變量名獲取
import { Component, ViewChild, AfterViewInit } from '@angular/core';
@Component({
? selector: 'my-app',
? template: `
? ? <h1>Welcome to Angular World</h1>
? ? <p #greet>Hello {{ name }}</p>
? `,
})
export class AppComponent {
? name: string = 'Semlinker';
? @ViewChild('greet')
? greetDiv: ElementRef;
? ngAfterViewInit() {
? ? console.log(this.greetDiv.nativeElement);
? }
}但我發(fā)現(xiàn)用這種方法獲取ngIf渲染的元素時(shí)得到的是undefined
<div *ngIf="isButtnGrop" (click)="dropBtnClick($event)">
<div cdkDropList #dropList [cdkDropListConnectedTo]="_connectableDropLists" (cdkDropListDropped)="drop($event)">
<div *ngFor="let item of itemDatas" (click)="onItemClick($event,item)" cdkDrag
(cdkDragStarted)="startDragging($event)" [cdkDragData]="{ item }">
</div>
</div>
</div>將static改成false 獲取
@ViewChild('dropList', { read: CdkDropList, static: false }) dropList: CdkDropList;
ngAfterViewInit(): void {
? ? if (this.dropList) {
? ? ? console.log(this.dropList)
? ? }
? }通過(guò)這個(gè)也是實(shí)現(xiàn)了一個(gè)buttonGroup拖拽button到 列表的功能,列表的button也能拖拽到 buttonGroup
用的也是Angular自帶的 cdk/drag-drop
import { CdkDragDrop, CdkDropList, moveItemInArray } from '@angular/cdk/drag-drop';自己實(shí)現(xiàn)的思路
官網(wǎng)的文檔和demo比較簡(jiǎn)單,沒(méi)有講到跨組件的實(shí)現(xiàn),簡(jiǎn)單記錄一下自己實(shí)現(xiàn)的思路。
將需要拖拽的元素加入cdkDropList,并且在A組件和B組件都初始化的時(shí)候獲取到需要拖拽的dom元素,將他們各自注冊(cè)到store中,帶上特殊的componentId。
A、B組件加上cdkDropListConnectedTo 這決定著組件可以跨組件拖動(dòng)到哪里,用_connectableDropLists變量。同樣的,在頁(yè)面初始化時(shí),通過(guò)rxjs的流訂閱特殊的componentId,去獲取到當(dāng)有拖拽list注冊(cè)到store中時(shí)的變化,并且賦值給_connectableDropLists數(shù)組。
const parentId = this.storeService.getProperty(this.pageId, this.componentId, 'parentId');
this.dragDropService.getDragListsAsync(this.pageId, parentId.value)
? ? ? .pipe(takeUntil(this.destroy))
? ? ? .subscribe(dropLists => {
? ? ? ? this._connectableDropLists = dropLists || [];
? ? ? });
this.storeService.getPropertyAsync(this.pageId, this.componentId, 'children')
? ? ? .pipe(takeUntil(this.destroy)).subscribe(result => {
? ? ? ? if (!result || result.length === 0) {
? ? ? ? ? this._children = [];
? ? ? ? ? this._dragData = [];
? ? ? ? ? this.changeRef.markForCheck();
? ? ? ? } else {
? ? ? ? ? const dropbuttonArray = result.filter((item) => {
? ? ? ? ? ? const itemType = this.storeService.getProperty(this.pageId, item, 'componentType');
? ? ? ? ? ? if (itemType === AdmComponentType.DropdownButton) return item;
? ? ? ? ? });
? ? ? ? ? if (dropbuttonArray.length > 0) {
? ? ? ? ? ? this._connectableDropLists = [];
? ? ? ? ? ? dropbuttonArray.forEach(comId => {
? ? ? ? ? ? ? this.dragDropService.getDragListsAsync(this.pageId, comId)
? ? ? ? ? ? ? ? .pipe(takeUntil(this.destroy))
? ? ? ? ? ? ? ? .subscribe(dropLists => {
? ? ? ? ? ? ? ? ? this._connectableDropLists.push(...dropLists);
? ? ? ? ? ? ? ? });
? ? ? ? ? ? });
? ? ? ? ? }
? ? ? ? }
? ? ? });因?yàn)锳組件是B組件的父級(jí),所以需要通過(guò)當(dāng)前組件id獲取到父級(jí)id,再獲取到拖拽元素
通過(guò)cdkDragData 把拖拽的元素的value,id等值帶上
通過(guò)(cdkDropListDropped)="drop($event)",注冊(cè)拖拽結(jié)束的回調(diào)事件
drop回調(diào)事件處理拖拽結(jié)束后的數(shù)據(jù)處理,這里涉及到項(xiàng)目低代碼的一些組件數(shù)據(jù)處理,大致是刪除oldParent children, 然后新的parent節(jié)點(diǎn)加上,再更改當(dāng)前組件的parent節(jié)點(diǎn)。同時(shí)這里涉及到buttongroup下面的button本身也可以互相拖拽的處理,所以也需要一層判斷來(lái)特殊處理。
drop(event: CdkDragDrop<any>) {
? ? if (event.previousContainer != event.container) {
? ? ? const { eventData } = event.item.data;
? ? ? const componentId = eventData[event.previousIndex];
? ? ? const oldParentId = this.storeService.getProperty(this.pageId, componentId, 'parentId', false)?.value;
? ? ? // delete oldParent children
? ? ? const oldParent = this.storeService.getProperties(this.pageId, oldParentId);
? ? ? const index = oldParent.children.indexOf(componentId);
? ? ? oldParent.children.splice(index, 1);
? ? ? // add newParent children
? ? ? const oldChildren = this.itemDatas.map(x => x.id.value);
? ? ? oldChildren.splice(event.currentIndex, 0, componentId);
? ? ? this.storeService.setProperty(this.pageId, componentId, 'parentId', { value: this.componentId }, [[this.pageId, componentId]]);
? ? ? this.storeService.setProperty(this.pageId, oldParentId, 'children', oldParent.children, [[this.pageId, oldParentId]]);
? ? ? this.storeService.setProperty(this.pageId, this.componentId, 'children', oldChildren);
? ? ? this.changeDetector.markForCheck();
? ? ? return;
? ? }
? ? moveItemInArray(this.itemDatas, event.previousIndex, event.currentIndex);
? ? const children = this.itemDatas.map(x => x.id.value);
? ? this.storeService.setProperty(this.pageId, this.componentId, 'children', children);
? }這樣子組件和父組件的內(nèi)部元素互相拖拽,也就能實(shí)現(xiàn)了
以上就是Angular獲取ngIf渲染的Dom元素示例的詳細(xì)內(nèi)容,更多關(guān)于Angular獲取ngIf渲染的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
快速學(xué)習(xí)AngularJs HTTP響應(yīng)攔截器
任何時(shí)候,如果我們想要為請(qǐng)求添加全局功能,例如身份認(rèn)證、錯(cuò)誤處理等,在請(qǐng)求發(fā)送給服務(wù)器之前或服務(wù)器返回時(shí)對(duì)其進(jìn)行攔截,是比較好的實(shí)現(xiàn)手段2015-12-12
AngularJS自定義過(guò)濾器用法經(jīng)典實(shí)例總結(jié)
這篇文章主要介紹了AngularJS自定義過(guò)濾器用法,結(jié)合實(shí)例形式總結(jié)分析了AngularJS自定義過(guò)濾器進(jìn)行包含、替換、篩選、過(guò)濾、排序等操作相關(guān)實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下2018-05-05
強(qiáng)大的 Angular 表單驗(yàn)證功能詳細(xì)介紹
本篇文章主要介紹了強(qiáng)大的 Angular 表單驗(yàn)證功能詳細(xì)介紹,使用 Angular 的內(nèi)置表單校驗(yàn)?zāi)軌蛲瓿山^大多數(shù)的業(yè)務(wù)場(chǎng)景的校驗(yàn)需求,有興趣的可以了解一下2017-05-05
angular中的http攔截器Interceptors的實(shí)現(xiàn)
本篇文章主要介紹了angular中的http攔截器Interceptors的實(shí)現(xiàn)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02
angular.js實(shí)現(xiàn)購(gòu)物車(chē)功能
這篇文章主要為大家詳細(xì)介紹了angular.js購(gòu)物車(chē)功能的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Angular實(shí)現(xiàn)的內(nèi)置過(guò)濾器orderBy排序與模糊查詢(xún)功能示例
這篇文章主要介紹了Angular實(shí)現(xiàn)的內(nèi)置過(guò)濾器orderBy排序與模糊查詢(xún)功能,涉及AngularJS過(guò)濾器、排序及字符串遍歷、查詢(xún)等相關(guān)操作技巧,需要的朋友可以參考下2017-12-12
解決angular 使用原生拖拽頁(yè)面卡頓及表單控件輸入延遲問(wèn)題
這篇文章主要介紹了angular 中使用原生拖拽頁(yè)面卡頓及表單控件輸入延遲問(wèn)題,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04

