AngularMaterial拖放事件返回错误的拖动项索引并移动错误的项。
创始人
2024-10-28 11:01:30
0

这个问题通常是由于使用了Angular Material的CDK拖放库中的错误导致的。解决此问题的一种方法是使用CDK DropList中提供的data参数来传递所需的数据。

以下是一个示例代码,演示如何使用data参数来解决此问题:

HTML模板部分:

{{ item.name }}

组件部分:

import { Component } from '@angular/core'; import { CdkDragDrop, moveItemInArray } from '@angular/cdk/drag-drop';

@Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { items = [ { id: 1, name: 'Item 1' }, { id: 2, name: 'Item 2' }, { id: 3, name: 'Item 3' }, { id: 4, name: 'Item 4' }, { id: 5, name: 'Item 5' } ];

onDrop(event: CdkDragDrop) { const draggedIndex = event.previousIndex; const droppedIndex = event.currentIndex; const draggedItem = event.item.data;

console.log('draggedIndex', draggedIndex);
console.log('droppedIndex', droppedIndex);
console.log('draggedItem', draggedItem);

moveItemInArray(this.items, draggedIndex, droppedIndex);

} }

在这个示例中,我们在CDK拖动事件中使用了data属性来传递所需的数据。然后,在onDrop方法中获取拖动项的数据,并使用它执行任何必要的操作。这样就能够解决Angular Material拖放事件返回错误索引和移动错误项的问题。

相关内容

热门资讯

第一分钟辅助!wepoker破... 第一分钟辅助!wepoker破解器,菠萝辅助器免费版的特点,经验教程(有挂规律)1、玩家可以在菠萝辅...
5分钟辅助!约局吧德州真的有透... 5分钟辅助!约局吧德州真的有透视挂吗,we-poker软件,妙计教程(今日头条)约局吧德州真的有透视...
第九分钟辅助!wpk插件,hh... 第九分钟辅助!wpk插件,hh poker辅助有用吗,资料教程(确实有挂)1、首先打开hh poke...
第三分钟辅助!wepoker透... 第三分钟辅助!wepoker透视脚本苹果版,pokemmo辅助器,教程书教程(确实有挂)1、完成we...
第三分钟辅助!哈糖大菠萝能开挂... 第三分钟辅助!哈糖大菠萝能开挂吗,aapoker脚本,法门教程(有人有挂)1、超多福利:超高返利,海...
两分钟辅助!wepoker游戏... 两分钟辅助!wepoker游戏的安装教程,wepoker透视苹果系统,法子教程(确实有挂)1、这是跨...
七分钟辅助!德扑之心免费透视,... 七分钟辅助!德扑之心免费透视,wepoker私人辅助器,方案教程(有挂教学)1、德扑之心免费透视免费...
三分钟辅助!hhpoker可以... 三分钟辅助!hhpoker可以开挂吗,wepoker私人局透视教程,操作教程(有挂讲解)1、wepo...
4分钟辅助!wepoker辅助... 4分钟辅助!wepoker辅助工具,hhpoker透视方法,教程书教程(有挂教程)1、起透看视 we...
9分钟辅助!wepoker怎么... 9分钟辅助!wepoker怎么获得好牌,pokerworld破解版下载,练习教程(有挂分析)1、不需...