Differentiate cdkDrag and click in angular
Asked Answered
G

1

6

I have a draggable element for which I have used cdkDrag and it is working fine for me. Now, I need to toggle a flag on click of the element. But when I drag the element and on drop the click event triggers. Can you please suggest how to work with cdkDrag and click together. Sharing the code snippet for the same.

        <div class="draggable-content" cdkDragBoundary=".drop-area" cdkDrag>
          <div class="min-workspace-view" *ngIf="showMinWorkspace === true">
            <mat-icon svgIcon="workspace" class="workspace-icon"
              (click)="$event.stopPropagation(); showMinWorkspace = !showMinWorkspace">
            </mat-icon>
          </div>
        </div>

The expectation is: when the element dragged then click should not trigger. when the element is just clicked then drag event should not trigger.

Galloglass answered 7/7, 2019 at 19:21 Comment(2)
same question with me. Looking for an answer. Did you solve this?Blackington
i've posted a different solution here: https://mcmap.net/q/1631596/-click-event-firing-after-draggingStrip
B
19

I got a solution to this.

You need a boolean property. Let's call it dragging.

On your cdkDrag element, add a cdkDragStarted callback, which sets this property to true. Then in your click callback, if that this.dragging is true, then set it to false and return. Hey Presto!

working stackblitz here https://stackblitz.com/edit/angular-drag-drop-and-clickable-element

Blackington answered 18/8, 2019 at 16:32 Comment(1)
Thanks @Joey, I have resolved this issue exactly the same way. Any how marking your answer as correct one. Thanks for the helpGalloglass

© 2022 - 2024 — McMap. All rights reserved.