我正在为我的项目使用 ng2-dragula,它对我来说效果很好。但现在我想在将元素放入袋子的同时传递数据。
我在互联网上搜索并找到了dragulaModel
但是当我尝试使用它时,我的代码不起作用。我已经在我的模板中完成了以下操作:
<li *ngFor="#comp of record.components | search:searchComp:true" [dragula]='"row-bag"' [dragulaModel]='record.components'>
<p class="text-center h6" style="font-size:8px;color:blue;font-weight:bold;"> {{comp.name }}</p>
</li>
在我的组件中:
dragulaService.dropModel.subscribe((value:any[]) => {
console.log(value);
this.dragulaService.onDropModel(value.slice(1));
});
dragulaService.removeModel.subscribe((value:any[]) => {
console.log(value);
this.dragulaService.onRemoveModel(value.slice(1));
});
dragulaService.drag.subscribe((value: any[]) => {
//console.log(`drag: ${value[0]}`);
this.onDrag(value.slice(1));
});
dragulaService.drop.subscribe((value: any[]) => {
//console.log(`drop: ${value[0]}`);
this.onDrop(value.slice(1));
});
dragulaService.over.subscribe((value: any[]) => {
//console.log(`over: ${value[0]}`);
this.onOver(value.slice(1));
});
dragulaService.out.subscribe((value: any[]) => {
//console.log(`out: ${value[0]}`);
this.onOut(value.slice(1));
});
dragulaService.setOptions('row-bag', {
copy: true
});
但是我收到此错误:
EXCEPTION: TypeError: Cannot read property '1' of undefined
此错误消息显示元素从具有指定模型的袋子"A"掉落到没有模型的袋子"B"中。您需要为尝试放入元素的包"B"指定"dragulaModel"。
例:
<div [dragula]='"bag-a"' [dragulaModel]='records' >
...
</div>
<div [dragula]='"bag-b"' [dragulaModel]='records' >
..
</div>