要防止将拖动的项目从一个列表拖动到另一个列表,可以使用Angular CDKDragDrop中的dropContainer属性。
示例源列表:
{{item}}
示例目标列表:
{{item}}
onDrop(event: CdkDragDrop) {
// Get the container ID of the drop list the item was dropped into
const currentContainerId = event.container.element.nativeElement.id;
// If the item was dropped into the same list it came from
if (event.previousContainer === event.container) {
// Do something
}
// If the item was dropped into a different list
else if (currentContainerId !== event.previousContainer.element.nativeElement.id) {
// Prevent the item from being dropped into the different list
return;
}
// Do something else
}
在这个例子中,我们将从每个列表中获取相应的dropContainer ID,并检查是否匹配。如果当前的dropContainer ID与之前的dropContainer