您需要在您的项目的父容器中移动您的 dragula 指令,如下所示:
<div class="container" [dragula]='"bag-one"' [dragulaModel]='items'>
<div [attr.id]="item.id" [attr.title]="i" class="card-item" *ngFor="let item of items; let i=index">
<item-detail></item-detail>
</div>
</div>
在你的Component.ts
let [el, target, source] = args;
console.log(el.id);
console.log(el.title);
您还可以在 Item-Detail 组件中使用 @Input 来输入所需的 id 和位置。
<item-detail [iteminfo]="item"></item-detail>
在你的 Component.ts
Import {Component, Input} from '@angular/core';
@Component({....});
@Input() iteminfo: Item;
这是我在项目中所做的。对于我的父组件:
import { Component, Input } from '@angular/core';
import { Card } from '../model/card';
import { Item } from '../model/item';
import { dragula, DragulaService } from 'ng2-dragula/ng2-dragula';
@Component({
selector: 'card', //parent component for item component
template: `
<div class="items"[dragula]='"bag-one"' [dragulaModel]='card.items'>
<div class="card-item" *ngFor="let item of card.items; let i = index; trackBy item?.item_Id">
<item [item]="item" [index]="i"></item>
</div>
</div>
})
export class CardComponent {
constructor(private dragulaService: DragulaService)
{
dragulaService.setOptions('bag-one', {
revertOnSpill: true
});
dragulaService.drop.subscribe((value) => {
this.onDrop(value.slice(1));
});
}
}
对于项目组件:
import { Component, Input } from '@angular/core';
import { Item } from '../model/item';
@Component({
selector: 'item',
template: `
{{item.title}} {{index}}
`
})
export class ItemComponent implements OnInit {
constructor(private itemSvc:ItemService) {}
private _index: number;
@Input()item: Item;
@Input()
set index(i: number) {
this._index = i;
// Do something here like save to database.
console.log('item index changed: ', this.item.title + i);
}
// Getter for the index Input property
get index(): number{
return this._index;
}
}
最后说明:请参阅 angular.io 站点上 CookBook -> Components Interaction https://angular.io/docs/ts/latest/cookbook/下的“使用 setter 拦截输入属性更改”