国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

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

瀏覽:153日期:2022-06-10 08:36:00
目錄
  • Angular獲取普通Dom元素的方法
    • 通過模板變量名獲取
    • 將static改成false 獲取
  • 自己實現的思路
    • 通過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);
? }
}

但我發現用這種方法獲取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)
? ? }
? }

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

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

自己實現的思路

官網的文檔和demo比較簡單,沒有講到跨組件的實現,簡單記錄一下自己實現的思路。

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

A、B組件加上cdkDropListConnectedTo 這決定著組件可以跨組件拖動到哪里,用_connectableDropLists變量。同樣的,在頁面初始化時,通過rxjs的流訂閱特殊的componentId,去獲取到當有拖拽list注冊到store中時的變化,并且賦值給_connectableDropLists數組。

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組件的父級,所以需要通過當前組件id獲取到父級id,再獲取到拖拽元素

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

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

drop回調事件處理拖拽結束后的數據處理,這里涉及到項目低代碼的一些組件數據處理,大致是刪除oldParent children, 然后新的parent節點加上,再更改當前組件的parent節點。同時這里涉及到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);
? }

這樣子組件和父組件的內部元素互相拖拽,也就能實現了

以上就是Angular獲取ngIf渲染的Dom元素示例的詳細內容,更多關于Angular獲取ngIf渲染的資料請關注其它相關文章!

標簽: JavaScript
主站蜘蛛池模板: 亚洲 成人 欧美 自拍 | 午夜国产精品久久久久 | 99热久久国产精品免费看 | 久久精品99 | 国产大片免费天天看 | 99国产在线| 欧美激情精品久久久久久久九九九 | 美女视频黄a全部免费专区一 | 国产免费一级在线观看 | 在线国产高清 | 久久成人免费观看草草影院 | 欧美综合视频在线 | 一级aaa级毛片午夜在线播放 | 欧美视频亚洲 | 久久综合狠狠综合狠狠 | 黄色a毛片| 亚洲综合国产一区二区三区 | 亚洲国产小视频 | 91亚洲人成手机在线观看 | 欧美亚洲一区 | 欧美综合视频在线观看 | 欧美视频一区二区三区四区 | 欧美成视频无需播放器 | 美女黄18 | 日韩免费一区二区三区 | 国产精品三级国语在线看 | 国产欧美日韩视频在线观看 | 欧美最刺激好看的一级毛片 | 国产成人综合怡春院精品 | 国产一区二区三区久久精品小说 | 国产成人精品实拍在线 | 日本黄色大片免费观看 | 又粗又爽又色男女乱淫播放男女 | 男人天堂手机在线 | 国产精在线| 亚洲欧美精品一区天堂久久 | 亚洲最大情网站在线观看 | a毛片在线还看免费网站 | 国产精品亚洲玖玖玖在线靠爱 | 九九九国产在线 | 国产免费一级高清淫曰本片 |