我正在尝试获取(或计算)角度材料cdkDropList中放置的项目的x / y坐标。我研究了文档https://material.angular.io/cdk/drag-drop/api#CdkDragDrop,发现CdkDragDrop界面中应该有一个名为“ distance”的属性,但是在事件的控制台日志中找不到它:
drop(event: CdkDragDrop<string[]>) {
console.log(event);
}
我只找到了容器,currentIndex,item,previousContainer,previousIndex。 顺便说一句:我是angel的新手,而这个文档使我困惑
致谢和问候
答案 0 :(得分:1)
好吧,算了吧...属性距离是在8.0.1版本中引入的,而我在7.1.0上...
更新
要获得此版本中的位置,您可以使用cdkDragMoved事件,如下所示:
https://grokonez.com/frontend/angular/angular-7/angular-7-drag-and-drop-example-angular-material-cdk
html部分:
<div cdkDrag class="drag-box"
(cdkDragStarted)="dragStarted($event)"
(cdkDragEnded)="dragEnded($event)"
(cdkDragMoved)="dragMoved($event)"
>
drag me
</div>
<p>{{state}} {{position}}</p>
和TypeScript中的
import { CdkDragEnd, CdkDragStart, CdkDragMove } from '@angular/cdk/drag-drop';
...
export class DragComponent implements OnInit {
state = '';
position = '';
...
dragStarted(event: CdkDragStart) {
this.state = 'dragStarted';
}
dragEnded(event: CdkDragEnd) {
this.state = 'dragEnded';
}
dragMoved(event: CdkDragMove) {
this.position = `> Position X: ${event.pointerPosition.x} - Y: ${event.pointerPosition.y}`;
}
}