angular material 拖放中的重复项

Duplicate items in angular material drag and drop

angular material 中有什么方法可以防止下拉列表中出现重复项吗? 这是示例代码

https://stackblitz.com/edit/angular-xjex4y-43l7uh

我尝试使用 event.currentIndex 检查该项目是否已存在于数组中,但这是不正确的,因为有时我会得到错误的值。

event.container.data.included(event.container.data[event.currentIndex])

在 stackblitz 中,我需要使用 .indexOf() insead od .included(),因为有些东西不起作用

您可以使用 previousIndex 检查当前项目并检查项目是否已经存在,如果存在则 return 喜欢

 {
      let idx=event.container.data.indexOf(event.previousContainer.data[event.previousIndex]);
      if(idx != -1){
        return;//if item exist
      }
      copyArrayItem(event.previousContainer.data,
                        event.container.data,
                        event.previousIndex,
                        event.currentIndex);
    }

working demo