简体   繁体   中英

Whole mat-dialog becomes drag handle when using cdk drag-drop

I was trying to combine mat-dialog and drag-drop in Angular Material 9.
The dialog.html is:

<h1 mat-dialog-title cdkDrag cdkDragHandle cdkDragRootElement=".cdk-overlay-pane">
Title</h1>
<div>
  <p>What's your favorite animal?</p>
</div>
<div mat-dialog-actions>
  <button mat-stroked-button (click)="onExit()">Exit</button>
</div>

But it seems that the whole dialog becomes the drag handle instead of just the dialog-title part.

stackblitz: https://stackblitz.com/edit/angular-ivy-putdym

Could someone please give a hint?

Your problem is here cdkDragRootElement=".cdk-overlay-pane"

You are telling it that the root element to drag is the entire overlay pane (by specifying the class).

Just remove that line and it will default to the element it is on, or specify the class for the title cdkDragRootElement=".mat-dialog-title" . Either will work. The function of that directive is so you can put a smaller drag handle that drags a larger parent element.

Edit: I misunderstood what the desired functionality was, the solution is to basically seperate the cdkDrag directive and cdkDragHandle directive to different elements. Here is a modified version of your stackblitz:

https://stackblitz.com/edit/angular-ivy-ucdjl3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM