【发布时间】:2018-07-20 00:10:19
【问题描述】:
是否可以使 Angular 材质对话框可拖动?因为在搜索了很多时间之后,我没有找到一个非常明确的答案。
【问题讨论】:
标签: angular dialog angular-material
是否可以使 Angular 材质对话框可拖动?因为在搜索了很多时间之后,我没有找到一个非常明确的答案。
【问题讨论】:
标签: angular dialog angular-material
是的,这已包含在 Angular Material 版本 7+ 更新中,通过使用 cdkDragRootElement
这是从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
【讨论】: