亚洲免费在线视频-亚洲啊v-久久免费精品视频-国产精品va-看片地址-成人在线视频网

您的位置:首頁技術(shù)文章
文章詳情頁

Angular獲取ngIf渲染的Dom元素示例

瀏覽:207日期:2022-06-10 08:36:00
目錄
  • Angular獲取普通Dom元素的方法
    • 通過模板變量名獲取
    • 將static改成false 獲取
  • 自己實現(xiàn)的思路
    • 通過cdkDragData 把拖拽的元素的value,id等值帶上

Angular獲取普通Dom元素的方法

通過模板變量名獲取

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渲染的元素時得到的是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)
? ? }
? }

通過這個也是實現(xiàn)了一個buttonGroup拖拽button到 列表的功能,列表的button也能拖拽到 buttonGroup
用的也是Angular自帶的 cdk/drag-drop

import { CdkDragDrop, CdkDropList, moveItemInArray } from "@angular/cdk/drag-drop";

自己實現(xiàn)的思路

官網(wǎng)的文檔和demo比較簡單,沒有講到跨組件的實現(xiàn),簡單記錄一下自己實現(xiàn)的思路。

將需要拖拽的元素加入cdkDropList,并且在A組件和B組件都初始化的時候獲取到需要拖拽的dom元素,將他們各自注冊到store中,帶上特殊的componentId。

A、B組件加上cdkDropListConnectedTo 這決定著組件可以跨組件拖動到哪里,用_connectableDropLists變量。同樣的,在頁面初始化時,通過rxjs的流訂閱特殊的componentId,去獲取到當(dāng)有拖拽list注冊到store中時的變化,并且賦值給_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);
? ? ? ? ? ? ? ? });
? ? ? ? ? ? });
? ? ? ? ? }
? ? ? ? }
? ? ? });

因為A組件是B組件的父級,所以需要通過當(dāng)前組件id獲取到父級id,再獲取到拖拽元素

通過cdkDragData 把拖拽的元素的value,id等值帶上

通過(cdkDropListDropped)="drop($event)",注冊拖拽結(jié)束的回調(diào)事件

drop回調(diào)事件處理拖拽結(jié)束后的數(shù)據(jù)處理,這里涉及到項目低代碼的一些組件數(shù)據(jù)處理,大致是刪除oldParent children, 然后新的parent節(jié)點加上,再更改當(dāng)前組件的parent節(jié)點。同時這里涉及到buttongroup下面的button本身也可以互相拖拽的處理,所以也需要一層判斷來特殊處理。

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)部元素互相拖拽,也就能實現(xiàn)了

以上就是Angular獲取ngIf渲染的Dom元素示例的詳細(xì)內(nèi)容,更多關(guān)于Angular獲取ngIf渲染的資料請關(guān)注其它相關(guān)文章!

標(biāo)簽: JavaScript
主站蜘蛛池模板: 精品成人在线观看 | 成人在线免费视频播放 | 欧美在线观看高清一二三区 | a黄网站| 手机看片在线精品观看 | 精品亚洲成a人在线播放 | 日韩毛片久久91 | 一级毛片a免费播放王色 | 男人的天堂在线观看免费 | 99精品久久99久久久久 | 日本天堂网| 国产精品99久久久久久人 | 欧美日韩一区二区三区在线视频 | 亚洲一区二区三区四区在线 | 国产真实搭讪系列 | 欧美一级高清免费播放 | 玖玖国产在线 | 一级成人毛片免费观看 | 久久橹 | 2021国产成人精品久久 | 久久国产精品-国产精品 | 男人女人做黄刺激性视频免费 | 免费99热在线观看 | 欧美成人影院免费观 | 高清性色生活片欧美在线 | 99久久久精品免费观看国产 | 成人午夜视频在线播放 | 国产亚洲午夜精品a一区二区 | 亚洲国产日韩综合久久精品 | 精品国产成人系列 | 国产一级片播放 | 欧美在线不卡 | 欧美三级三级三级爽爽爽 | 欧美一级专区免费大片 | 精品91精品91精品国产片 | 成人网在线免费观看 | 日韩美女在线看免费观看 | 美女被cao免费看在线看网站 | 国产欧美日韩一区 | 手机看片神马午夜 | 精品女厕沟底拍撒尿 |