How to Create simple drag and Drop in angularjs

后端 未结 8 1605
孤独总比滥情好
孤独总比滥情好 2020-11-29 16:55

I want to know how to do drag and drop by using AngularJs.

This is what I have so far:



        
8条回答
  •  借酒劲吻你
    2020-11-29 17:26

    Using HTML 5 Drag and Drop
    You can easily archive this using HTML 5 drag and drop feature along with angular directives.

    1. Enable drag by setting draggable = true.
    2. Add directives for parent container and child items.
    3. Override drag and drop functions - 'ondragstart' for parent and 'ondrop' for child.

    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

提交回复
热议问题