[cdkDragDrop] CopyArrayItem: items somehow connected
I dont now if i am stupid or this is a bug... i asked several people and no one seems to know the answer.
What are the steps to reproduce?
https://stackblitz.com/edit/angular-yr8ej6
1.) drag one item to the right container 2.) click on test button
What is the expected behavior?
click on test button should only change the value of the item in the right container on position 0 of the array
What is the current behavior?
click on test button changes both values
Which versions of Angular, Material, OS, TypeScript, browsers are affected?
Angular: 7.2.8 @angular/cdk 7.3.7
It's because copyArrayItem only adds a reference to the new array, it doesn't actually do a deep copy of the object.
thanks for the answer, is there a simple way to make a real copy?
The CDK doesn't provide a deep copy function out-of-the-box, but there are lots of solutions out there. E.g. something like this should do the trick in lodash:
import {cloneDeep} from 'lodash';
onTalkDrop(event: CdkDragDrop<Element[]>) {
if (event.previousContainer !== event.container){
// Clone the item that was dropped.
const clone = cloneDeep(event.previousContainer.data[event.previousIndex]);
// Add the clone to the new array.
event.container.data.splice(event.currentIndex, 0, clone);
}
else{
moveItemInArray(
event.container.data,
event.previousIndex,
event.currentIndex
);
}
}
THANKS a lot :D works like a charm.
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.