试试这样的:
DEMO
<div class="listArea">
<h4> Drag and Drop list in Green Area: </h4>
<ul class="unstyle">
<li *ngFor="let li of List; let i = index" id="drag{{i}}" draggable="true" (dragstart)="drag($event, i) ">
i am list {{li.name}}
</li>
</ul>
</div>
<div class="buildArea " id="div1 " (drop)="drop($event) " (dragover)="allowDrop($event) ">
<h4> Drop Here </h4>
<ul class="unstyle">
<li *ngFor="let li of DraggedList; let i = index" id="dragged{{i}}">
<p>{{li.innerHTML}}</p>
<button (click)="deleteItem(i)" type="button"> X</button>
</li>
</ul>
</div>
电话号码:
List: Array<any> = [
{ name: 1 },
{ name: 2 },
{ name: 3 },
{ name: 4 },
{ name: 5 },
]
elId: number;
DraggedList: Array<any> = [
]
allowDrop(ev) {
ev.preventDefault();
}
drag(ev, i) {
this.elId = i;
ev.dataTransfer.setData("text", ev.target.id);
}
drop(ev) {
ev.preventDefault();
var data = ev.dataTransfer.getData("text");
// ev.target.appendChild(document.getElementById(data).cloneNode(true));
this.DraggedList.push(document.getElementById(data));
}
deleteItem(i) {
if (i > -1) {
this.DraggedList.splice(i, 1);
}
}