电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

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

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

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

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)文章!

標(biāo)簽: JavaScript
主站蜘蛛池模板: 对夹式止回阀_对夹式蝶形止回阀_对夹式软密封止回阀_超薄型止回阀_不锈钢底阀-温州上炬阀门科技有限公司 | 超声波焊接机_超音波熔接机_超声波塑焊机十大品牌_塑料超声波焊接设备厂家 | 打包箱房_集成房屋-山东佳一集成房屋有限公司 | 老城街小面官网_正宗重庆小面加盟技术培训_特色面馆加盟|牛肉拉面|招商加盟代理费用多少钱 | 复合土工膜厂家|hdpe防渗土工膜|复合防渗土工布|玻璃纤维|双向塑料土工格栅-安徽路建新材料有限公司 | 缝纫客 | 浙江栓钉_焊钉_剪力钉厂家批发_杭州八建五金制造有限公司 | 青岛空压机,青岛空压机维修/保养,青岛空压机销售/出租公司,青岛空压机厂家电话 | 闭端端子|弹簧螺式接线头|防水接线头|插线式接线头|端子台|电源线扣+护线套|印刷电路板型端子台|金笔电子代理商-上海拓胜电气有限公司 | ★店家乐|服装销售管理软件|服装店收银系统|内衣店鞋店进销存软件|连锁店管理软件|收银软件手机版|会员管理系统-手机版,云版,App | 钢结构厂房造价_钢结构厂房预算_轻钢结构厂房_山东三维钢结构公司 | 铸钢件厂家-铸钢齿轮-减速机厂家-淄博凯振机械有限公司 | 洁净棚-洁净工作棚-无菌室-净化工程公司_北京卫护科技有限公司 | 附着力促进剂-尼龙处理剂-PP处理剂-金属附着力处理剂-东莞市炅盛塑胶科技有限公司 | 曙光腾达官网-天津脚手架租赁-木板架出租-移动门式脚手架租赁「免费搭设」 | 刚性-柔性防水套管-橡胶伸缩接头-波纹管补偿器-启腾供水材料有限公司 | 房间温控器|LonWorks|海思 | 上海橡胶接头_弹簧减震器_金属软接头厂家-上海淞江集团 | 伶俐嫂培训学校_月嫂培训班在哪里报名学费是多少_月嫂免费政府培训中心推荐 | 锯边机,自动锯边机,双面涂胶机-建业顺达机械有限公司 | 纯化水设备-纯水设备-超纯水设备-[大鹏水处理]纯水设备一站式服务商-东莞市大鹏水处理科技有限公司 | 带锯机|木工带锯机圆木推台锯|跑车带锯机|河北茂业机械制造有限公司| | 集装箱展厅-住人集装箱住宿|建筑|房屋|集装箱售楼处-山东锐嘉科技工程有限公司 | 标准光源箱|对色灯箱|色差仪|光泽度仪|涂层测厚仪_HRC大品牌生产厂家 | 微型气象仪_气象传感器_防爆气象传感器-天合传感器大全 | 上海洗地机-洗地机厂家-全自动洗地机-手推式洗地机-上海滢皓洗地机 | 大米加工设备|大米加工机械|碾米成套设备|大米加工成套设备-河南成立粮油机械有限公司 | 板式换热器_板式换热器价格_管式换热器厂家-青岛康景辉 | 电子万能试验机_液压拉力试验机_冲击疲劳试验机_材料试验机厂家-济南众标仪器设备有限公司 | 股票入门基础知识_股票知识_股票投资大师_格雷厄姆网 | 油液红外光谱仪-油液监测系统-燃油嗅探仪-上海冉超光电科技有限公司 | 济南网站策划设计_自适应网站制作_H5企业网站搭建_济南外贸网站制作公司_锐尚 | 顺景erp系统_erp软件_erp软件系统_企业erp管理系统-广东顺景软件科技有限公司 | 生鲜配送系统-蔬菜食材配送管理系统-连锁餐饮订货配送软件-挪挪生鲜供应链管理软件 | 过滤器_自清洗过滤器_气体过滤器_苏州华凯过滤技术有限公司 | 阜阳在线-阜阳综合门户| 定制奶茶纸杯_定制豆浆杯_广东纸杯厂_[绿保佳]一家专业生产纸杯碗的厂家 | 气体检测仪-氢气检测仪-可燃气体传感器-恶臭电子鼻-深国安电子 | 经济师考试_2025中级经济师报名时间_报名入口_考试时间_华课网校经济师培训网站 | 懂研帝_专业SCI论文润色机构_SCI投稿发表服务公司 | 糖衣机,除尘式糖衣机,全自动糖衣机,泰州市长江制药机械有限公司 体感VRAR全息沉浸式3D投影多媒体展厅展会游戏互动-万展互动 |