我正在使用Angular Material中的拖放事件来实现重叠检测,但是我还需要创建一条指令来监听 drag 事件:
DragDirective:
@Directive({
selector: '[appDrag]'
})
export class DragDirective {
constructor(private el: ElementRef, private renderer: Renderer2) {
this.renderer.setAttribute(el.nativeElement, 'draggable', 'true');
}
@HostListener('dragstart', ['$event'])
public onDragStart(ev: DragEvent) {
console.log(`onDragStart: `, ev); // doesn't work if cdkDrag attribute is placed on the element
}
}
HTML:
<div class="draggable" appDrag cdkDrag *ngFor="let keywordGroupCollection of keywordGroupsSelected; let i = index">
other elements here
</div>