angular - CdkDragDrop 和 ngTemplateOutlet

标签 angular drag-and-drop angular-material angular7 angular-material-7

我正在尝试使用与 Angular Material 7 相关的拖放功能。

我使用 ngTemplateOutlet 将模板分成可重复使用的部分每个选项都可以是 东西 ™ 或嵌套的 东西 ™ 还有更多子事物 ™。

嵌套 事情 ™ 显示为扩展面板。
我要全部一级事情 ™ 可以像列表一样重新排序。

(好吧,好吧,很明显这是一个带有正常和嵌套选项的可重新排序的sidenav,只是假装它不是那么明显)

这是我最初编写的代码。

<div cdkDropList (cdkDropListDropped)="dropItem($event)" lockAxis="y">
  <ng-container *ngFor="let thing of things">
      <ng-container
        *ngTemplateOutlet="!thing.children ? singleThing : multipleThing; context: { $implicit: thing }"
      ></ng-container>
    </ng-container>
</div>

<ng-template #singleThing let-thing>
  <div cdkDrag>
    <ng-container *ngTemplateOutlet="thingTemplate; context: { $implicit: thing }"></ng-container>
  </div>
</ng-template>

<ng-template #multipleOption let-thing>
  <mat-expansion-panel cdkDrag (cdkDropListDropped)="dropItem($event)">
    <mat-expansion-panel-header>
      <mat-panel-title>
        <p>Nested thing title</p>
        <span cdkDragHandle></span>
      </mat-panel-title>
    </mat-expansion-panel-header>

    <ng-container *ngFor="let childThing of thing.children">
      <div class="childThing">
        <ng-container *ngTemplateOutlet="thingTemplate; context: { $implicit: childThing }"></ng-container>
      </div>
    </ng-container>
  </mat-expansion-panel>
</ng-template>

<ng-template #thingTemplate let-thing>
  <p>I'm a thing!</p>
  <span cdkDragHandle></span>
</ng-template>

问题:单事情 ™ 是可拖动的,但它们并没有像 cdkDropList 那样强制执行,我可以将它们拖到任何地方。

前段时间我在尝试使用模板 socket 并放置 ng-template 时遇到了类似的问题回到“HTML 流程”可以解决这个问题,所以我也尝试了同样的方法。
<div cdkDropList (cdkDropListDropped)="dropItem($event)" lockAxis="y">
  <ng-container *ngFor="let thing of things">
      <ng-container
        *ngIf="!thing.children; then singleThing; else multipleThing"
      ></ng-container>
        <ng-template #singleThing>
          <div cdkDrag>
            <ng-container *ngTemplateOutlet="thingTemplate; context: { $implicit: thing }"></ng-container>
          </div>
        </ng-template>

        <ng-template #multipleOption>
          <mat-expansion-panel cdkDrag (cdkDropListDropped)="dropItem($event)">
            <mat-expansion-panel-header>
              <mat-panel-title>
                <p>Nested thing title</p>
                <span cdkDragHandle></span>
              </mat-panel-title>
            </mat-expansion-panel-header>

            <ng-container *ngFor="let childThing of thing.children">
              <div class="childThing">
                <ng-container *ngTemplateOutlet="thingTemplate; context: { $implicit: childThing }"></ng-container>
              </div>
            </ng-container>
          </mat-expansion-panel>
        </ng-template>
    </ng-container>
</div>

<ng-template #thingTemplate let-thing>
  <p>I'm a thing!</p>
  <span cdkDragHandle></span>
</ng-template>

而且,当然为什么不呢,它有效!
是的,很好,但是 为什么 ?

没有太大变化,我们使用了ngIf而不是第一个 ngTemplateOutlet并删除了 的上下文绑定(bind)东西 ™ 因为现在两个模板都具有其局部变量引用,这要归功于共享范围。

那么,为什么它以第二种方式而不是第一种方式工作呢?

加分点:是否有可能让它保持第一个代码结构在我看来显然更具可读性和简洁性?

最佳答案

我遇到了同样的问题,我什至将其报告为 issue on GitHub .

原来是cdkDropList的分离造成的来自 cdkDrag . cdkDrag必须在一个标签中,该标签嵌套在带有 cdkDropList 的标签中,否则被拖动的元素不会检测到放置区。

在您的情况下,解决方案将是一个额外的 <div cdkDrag>下面cdkDropList ,并且只有在此之下,您才能使用 ngTemplateOutlet 调用模板.

关于angular - CdkDragDrop 和 ngTemplateOutlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53210886/

相关文章:

angular-material - 选中的属性在 mat-radio-button 中不起作用

Angular 编译器找不到 jasmine.CustomMatchers,即使它在 @types/jasmine 中

Angular 4 项目 war ,当创建为 war 时,刷新时会产生白色标签错误

html - Bootstrap 导航粘性停留在页面顶部

html - 如何制作 flex 弹出窗口?

c++ - 想简单地制作一个复杂的 c++ gui

JavaScript:在链接上拖动鼠标

c# - WPF C# 拖放事件

javascript - 空注入(inject)器错误 : No provider for MatBottomSheetRef

angular - 我将如何在另一个组件中切换侧边栏?