I want to know how to do drag and drop by using AngularJs.
This is what I have so far:
Using HTML 5 Drag and Drop
You can easily archive this using HTML 5 drag and drop feature along with angular directives.
Find the example below in which list is array of items.
HTML code:
{{item}}
Javascript:
module.directive("draggableItem",function(){
return {
link:function(scope,elem,attr){
elem[0].ondragstart = function(event){
scope.$parent.selectedItem = scope.item;
};
}
};
});
module.directive("draggrbleContainer",function(){
return {
link:function(scope,elem,attr){
elem[0].ondrop = function(event){
event.preventDefault();
let selectedIndex = scope.list.indexOf(scope.$parent.selectedItem);
let newPosition = scope.list.indexOf(scope.item);
scope.$parent.list.splice(selectedIndex,1);
scope.$parent.list.splice(newPosition,0,scope.$parent.selectedItem);
scope.$apply();
};
elem[0].ondragover = function(event){
event.preventDefault();
};
}
};
});
Find the complete code here https://github.com/raghavendrarai/SimpleDragAndDrop