如何使角度材质对话框可拖动

时间:2018-02-09 09:03:36

标签: angular dialog angular-material

是否可以使Angular Material Dialog可拖动?因为在经历了大量的搜索时间后,我没有找到一个非常明确的答案。

1 个答案:

答案 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

相关问题