卡住超过2小时的角CDK拖动 - 无法将项目放在位置

发布于 2025-02-13 07:57:50 字数 1120 浏览 0 评论 0 原文

当刚开始进入Angular CDK DragnDrop时,我正在努力完成我的任务。试图拖动图像项目并落入位置时,我得到了无法读取未定义的属性(读取长度')

不确定这是因为与Angular Dragndrop CDK的不同界面

  drop(event: CdkDragDrop<any>) {
    if (event.previousContainer === event.container) {
      moveItemInArray(
        event.container.data,
        event.previousIndex,
        event.currentIndex
      );
    } else {
      transferArrayItem(
        event.previousContainer.data, 
        event.container.data,                              // getting undefined here
        event.previousIndex,
        event.currentIndex
      );

      console.log(
        'transferArrayItem',
        event.container.data.colorways
      );

      const formArry = this.myDivName;
      formArry.insert(0, this.fb.control(event.container.data));
    }
  }

是否事件。

我的stackblitz

Am Struggling lot to complete my task as newly started into Angular CDK DragnDrop. When trying to drag the image item and drop into location, I got Cannot read properties of undefined (reading 'length') .

Not sure this is because of different Interface against Angular DragnDrop CDK

  drop(event: CdkDragDrop<any>) {
    if (event.previousContainer === event.container) {
      moveItemInArray(
        event.container.data,
        event.previousIndex,
        event.currentIndex
      );
    } else {
      transferArrayItem(
        event.previousContainer.data, 
        event.container.data,                              // getting undefined here
        event.previousIndex,
        event.currentIndex
      );

      console.log(
        'transferArrayItem',
        event.container.data.colorways
      );

      const formArry = this.myDivName;
      formArry.insert(0, this.fb.control(event.container.data));
    }
  }

does event.container.data, pointing to drop location or source location?

My stackblitz

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冬天旳寂寞 2025-02-20 07:57:50

在这里,您可以

/* Add initial empty data */
imgSource: Pokedex = {
  colorways: [],
  count: 0,
};

drop(event: CdkDragDrop<any>) {
...
else {
 transferArrayItem(
   event.previousContainer.data,
   event.container.data, //remove colorways, event.container.data?.colorways,
   event.previousIndex,
   event.currentIndex
 );
}
<!-- Left container -->
[cdkDropListData]="colorways" <!-- Wrong -->
[cdkDropListData]="colorways.colorways" <!-- Right -->

<!-- Right Container -->
[cdkDropListData]="imgSource" <!-- Wrong -->
[cdkDropListData]="imgSource?.colorways" <!-- Right -->

工作stackblitz

Here you go

/* Add initial empty data */
imgSource: Pokedex = {
  colorways: [],
  count: 0,
};

drop(event: CdkDragDrop<any>) {
...
else {
 transferArrayItem(
   event.previousContainer.data,
   event.container.data, //remove colorways, event.container.data?.colorways,
   event.previousIndex,
   event.currentIndex
 );
}
<!-- Left container -->
[cdkDropListData]="colorways" <!-- Wrong -->
[cdkDropListData]="colorways.colorways" <!-- Right -->

<!-- Right Container -->
[cdkDropListData]="imgSource" <!-- Wrong -->
[cdkDropListData]="imgSource?.colorways" <!-- Right -->

Working Stackblitz

与他有关 2025-02-20 07:57:50

理解CDKDROPLIST的最佳方法是忘记CDKDROPLIST。我们需要简单地思考对象的两个数组

CDKDROPLIST只是一个接口,即更改数组元素的位置或将数组的一个元素移至另一个元素。我们可以想象两个简单的输入和三个按钮,第一个按钮互换一个数组中一个元素的位置,另一个按钮在另一个阵列中以及最后一个按钮互换两个阵列的元素

函数moveiteminArray和TransferararayItem只是JavaScript函数。我们可以看到定义 a>我们可以使用是否使用(*)

发出事件的$事件(CDKDroplistDropped)是 cdkdragdrop ,所以我们有

  1. 容器,cdkdroplist,cdkdroplist 强>掉落 - 所以我们有
    event.container.data我们在[数据]或
    CDKDROPLIST
  2. 先前查看器,关于CKDDROPLIST 的同上,我们从开始drag
  3. currentIndex,该元素的索引(位置)在数组中
    event.container.data 其中 droped
  4. 先前索引,元素的索引(位置)
    event.previouscontainer.data

so so,您的CDKDROPLIST的数据应为colorways.colorways和imgsource.colorways.colorways.colorways @sameer所说,您需要初始化OBJ imgsource

注意:在.html中使用.html,

<pre>
{{cdkDropTarget.data|json}}
</pre>

以查看“存储” CDKDROPLIST“目标”阵列, ”

(*)例如在要在一个数组中添加元素,在此另一个 cdkdroplist to cdkdroplist to cdkdroplist to cdkdroplist to cdkdroplist “数据”是一个对象的CDKDROPLIST

The best way to understand the cdkDropList is forget the cdkDropList. We need think simply in two arrays of object

The cdkDropList is only an interface to change the position of the elements of the arrays or to move one element of the array to the another one. We can imagine two simple inputs and three buttons, the first buttons interchange the position of one element in one array and in the another one and the last button interchange the elements of the two arrays

The functions moveItemInArray and transferArrayItem are only javascript function. We can see the definition this and we can use or not (*)

The $event that emit an event (cdkDropListDropped) is type of CdkDragDrop, so we have

  1. container, the cdkDropList where is dropped -so we have in the
    event.container.data the array we indicate in [data] or the
    cdkDropList
  2. previousContainer, idem about the ckdDropList from we start drag
  3. currentIndex, the index (position) of the element in array
    event.container.data where is dropped
  4. previousIndex, the index (position) of the element in array of
    event.previousContainer.data

So, the data of your cdkDropList should be colorways.colorways and imgSource.colorways as @Sameer says and you need initialize your obj imgSource

NOTE: use in .html

<pre>
{{cdkDropTarget.data|json}}
</pre>

To see the array that "store" the cdkDropList "target"

(*) e.g. in this stackblitz I used the a cdkDropList only to add elements to one array, in this another one drop elements in a CdkDropList to a group of cdkDropList who "data" is an object

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文