AngularDropnDropCDK与响应式表单一起工作时出现异常——项目会被复制。
创始人
2024-10-24 15:01:17
0

在处理拖放操作时,需要手动从数组中删除项目并重新排序。以下是示例代码:

// HTML模板

{{ item.controls.name.value }}

// 组件 export class MyComponent implements OnInit { items: FormArray;

constructor(private fb: FormBuilder) {}

ngOnInit() { this.items = this.fb.array([ { name: 'Item 1' }, { name: 'Item 2' }, { name: 'Item 3' }, { name: 'Item 4' }, ]); }

onDrop(event: CdkDragDrop) { if (event.previousContainer === event.container) { moveItemInFormArray( this.items, event.previousIndex, event.currentIndex ); } else { transferItemInFormArray( this.items, event.previousIndex, event.currentIndex ); } } }

// 帮助函数 export function moveItemInFormArray(array: FormArray, fromIndex: number, toIndex: number) { const item = array.at(fromIndex); array.removeAt(fromIndex); array.insert(toIndex, item); }

export function transferItemInFormArray(fromArray: FormArray, fromIndex: number, toIndex: number) { const item = fromArray.at(fromIndex); fromArray.removeAt(fromIndex);

const toArray = toIndex >= fromArray.length ? fromArray : fromArray.parent.get('items') as FormArray; toArray.insert(toIndex, item); }

相关内容

热门资讯

透视脚本!红龙poker辅助器... 您好,红龙poker辅助器免费观看这款游戏可以开挂的,确实是有挂的,需要了解加去威信【4852750...
透视插件!闲逸辅助软件下载(辅... 透视插件!闲逸辅助软件下载(辅助)决战卡五星辅助源码(一直真的有挂)-哔哩哔哩1、决战卡五星辅助源码...
透视app!hhpoker怎么... 透视app!hhpoker怎么破解(透视)HH平台挂(好像真的有透视)-哔哩哔哩进入游戏-大厅左侧-...
透视脚本!新畅游互娱科技(辅助... 透视脚本!新畅游互娱科技(辅助)新道游房间(切实一直都是有透视)-哔哩哔哩1、起透看视 新畅游互娱科...
透视工具!混沌休闲有没有挂(辅... 透视工具!混沌休闲有没有挂(辅助)胡乐麻将辅助(果然存在有挂)-哔哩哔哩1.混沌休闲有没有挂 选牌创...
透视教程!wepoker辅助透... 透视教程!wepoker辅助透视(透视)we-poker软件(确实真的是有辅助器)-哔哩哔哩1、在w...
透视工具!wepoker手机版... 透视工具!wepoker手机版辅助(透视)wepoker黑侠辅助器(确实真的有挂)-哔哩哔哩1、该软...
透视神器!花花生活圈怎么开挂(... 透视神器!花花生活圈怎么开挂(辅助)掌中乐辅助(一直有挂)-哔哩哔哩1、用户打开应用后不用登录就可以...
透视软件!wepoker透视有... 透视软件!wepoker透视有吗(透视)poker world辅助(果然一直都是有挂)-哔哩哔哩一、...
透视插件!wepoker透视辅... 透视插件!wepoker透视辅助下载(透视)wepoker辅助分析器(都是是有辅助器)-哔哩哔哩we...