简体   繁体   English

Angular cdk 拖放到 Material 对话框

[英]Angular cdk drag and drop into Material dialog

I am implementing a Drag and Drop using Angular cdk and am getting stuck trying to drag the element into a Dialog which is a child container of the container that contains the cdkDropListGroup.我正在使用 Angular cdk 实现拖放,并且在尝试将元素拖动到 Dialog 中时遇到了困难,该 Dialog 是包含 cdkDropListGroup 的容器的子容器。 To demonstrate the hierarchy graphically:以图形方式展示层次结构:

在此处输入图像描述

All the white boxes are components.所有的白盒都是组件。 The final goal is to be able to drag and drop elements between all Child components of WorkbenchComponent, including the ones that are generated programmatically.最终目标是能够在 WorkbenchComponent 的所有子组件之间拖放元素,包括以编程方式生成的子组件。

Obviously since they are not part of the "template tree" at compile time however, the WorkbenchComponent cdkDropListGroup does not include the dialogs.显然,由于它们在编译时不是“模板树”的一部分,但是 WorkbenchComponent cdkDropListGroup 不包括对话框。

Now I am struggling to find any way how to make the drag and drop into the SubwindowComponents work (using Material).现在我正在努力寻找如何使拖放到 SubwindowComponents 工作(使用材料)的任何方法。

HTML5 drag and drop as well as primeng drag and drop does not work unfortunately, due to security restrictions with certain browsers in the deployment context.不幸的是,HTML5 拖放和primeng 拖放不起作用,因为部署上下文中某些浏览器的安全限制。

Edit: Maybe something along the lines of ComponentFactoryResolver or ViewContainerRef could work?编辑:也许类似于 ComponentFactoryResolver 或 ViewContainerRef 的东西可以工作?

Providing more information about (what I believe to be) the approach suggested by @TotallyNewb.提供有关(我认为是)@TotallyNewb 建议的方法的更多信息。 (And what I would choose as the accepted answer) (我会选择什么作为接受的答案)

The solution is rather simple.解决方案相当简单。 In the component that generates the materials DialogComponent instances (here: the SidebarSubwindowComponent), we need to add the ViewContainerRef to the MatDialogConfig object that is passed to the open method of the dialog.在生成材质 DialogComponent 实例的组件中(此处为 SidebarSubwindowComponent),我们需要将 ViewContainerRef 添加到传递给对话框的 open 方法的 MatDialogConfig object 中。 This ViewContainerRef can be passed on to all grandchildren.这个 ViewContainerRef 可以传递给所有的孙子。 In code:在代码中:

  1. Parent component (member of cdkDropListGroup)父组件(cdkDropListGroup 的成员)
import { Component, OnInit, ViewContainerRef } from '@angular/core';

export class ParentComponent implements OnInit {

  constructor (private viewContainerRef: ViewContainerRef) { }

  openDialog() {
    const dialogConfig = new MatDialogConfig();
    dialogConfig.viewContainerRef = this.viewContainerRef;

    let childDialogComponent = ChildDialogComponent;

    const dialogRef = this.dialog.open(childDialogComponent , dialogConfig);
    
    dialogRef.afterClosed().subscribe();
  }
}
  1. Child Component (with Dialog)子组件(带对话框)
import { Component, OnInit, Inject, ViewContainerRef } from '@angular/core';

export class SubwindowTableComponent implements OnInit {

  constructor (private viewContainerRef: ViewContainerRef) {}

  showGrandChild() {
    const dialogConfig = new MatDialogConfig();
    dialogConfig.viewContainerRef = this.viewContainerRef;
    const dialogRef = this.dialog.open(GrandChildDialogComponent, dialogConfig);
    // and so on and so forth
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM