是否可以使Angular Material Dialog可拖动?因为在经历了大量的搜索时间后,我没有找到一个非常明确的答案。
答案 0 :(得分:1)
是的,并且使用 cdkDragRootElement
将其包含在Angular Material 7+版本中。以下是从material.angular.io复制的示例
HTML:
<button (click)="openDialog()">Open a draggable dialog</button>
<ng-template>
<div class="example-dialog-content" cdkDrag cdkDragRootElement=".cdk-overlay-pane">
Drag the dialog around!
</div>
</ng-template>
TS:
export class CdkDragDropRootElementExample implements AfterViewInit, OnDestroy {
@ViewChild(TemplateRef) _dialogTemplate: TemplateRef<any>;
private _overlayRef: OverlayRef;
private _portal: TemplatePortal;
constructor(private _overlay: Overlay, private _viewContainerRef: ViewContainerRef) {}
ngAfterViewInit() {
this._portal = new TemplatePortal(this._dialogTemplate, this._viewContainerRef);
this._overlayRef = this._overlay.create({
positionStrategy: this._overlay.position().global().centerHorizontally().centerVertically(),
hasBackdrop: true
});
this._overlayRef.backdropClick().subscribe(() => this._overlayRef.detach());
}
ngOnDestroy() {
this._overlayRef.dispose();
}
openDialog() {
this._overlayRef.attach(this._portal);
}
}
Stackblitz: Draggable Dialog