我的应用程序中的每个对话框或多或少都是这样的:
<custom-dialog-container>
<custom-dialog-header>...</custom-dialog-header>
<custom-dialog-content>
...
</custom-dialog-content>
<custom-button>
...
</custom-button>
</custom-dialog-container>
我在每个对话框中使用一个custom-dialog-header
指令,最近我发现了通过这样的指令将可拖放的效果应用于对话框的方法:
import { DragDrop } from '@angular/cdk/drag-drop';
import { Directive, ElementRef, HostBinding } from '@angular/core';
@Directive({
selector: 'custom-dialog-header, [customDialogHeader]'
})
export class CustomDialogHeaderDirective {
@HostBinding('class') class = 'dialog__header';
constructor(
element: ElementRef<HTMLElement>,
dragDrop: DragDrop
){
const availablePanes = document.getElementsByClassName('cdk-overlay-pane');
const latestPane = availablePanes.item(availablePanes.length - 1);
const dragRef = dragDrop.createDrag(element);
dragRef.withRootElement(latestPane as HTMLElement);
}
}
到目前为止一切都很好,因为一切都和预期的一样。
唯一缺少的是只对<custom-dialog-header></custom-dialog-header>
.的句柄区域限制。
现在,它被设置为整个对话框(甚至从custom-dialog-content
部分拖动一个输入也可以拖动整个对话框)。
所以这是我的问题-我应该如何实现一个?我读过许多关于这个主题和文档的文章,但是没有发现任何帮助。
发布于 2021-05-31 06:04:28
可以使用dragRef.withHandles([element])
为特定元素创建句柄。这个应该能解决你的案子。
https://stackoverflow.com/questions/67773381
复制