Search code examples
angulardrag-and-drop

How to use the method "getFreeDragPosition" for Angular 8 CdkDrag?


There is a method definition in Angular CdkDrag API. enter image description here

But how to call it in code?

I have tried like below but error is happening. What is the right way to use this kind of methods?

export class DragableComponent implements OnInit {

  _dragRef: DragRef<CdkDrag>;


   this._dragRef.getFreeDragPosition();

Solution

  • This is a method that is called on an instance of CdkDrag. One of the ways to get the instance is to use cdkDragEnded output event.

    Sample code:

    public onDragEnded(event: CdkDragEnd): void {
      console.log(event.source.getFreeDragPosition()); // returns { x: 0, y: 0 }
    }
    <p cdkDrag (cdkDragEnded)="onDragEnded($event)">
      Draggable paragraph
    </p>