top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration

Facebook Login
Site Registration

How to Create the drop-target directive in angularJs?

0 votes
60 views
How to Create the drop-target directive in angularJs?
posted Feb 1, 2017 by Ranjith Havaldar

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
 
Best answer

Create two directives: Draggable and DropTarget. Items that going to be “dragged” are decorated with Draggable directive and the container where the items are dropped is decorated with DropTarget directive.

// DraggableDirective.js
var Draggable = function () {

    return {
        restrict: "A",
        link: function(scope, element, attributes, ctlr) {
            element.attr("draggable", true);

            element.bind("dragstart", function(eventObject) {
                eventObject.originalEvent.dataTransfer.setData("text", attributes.itemid);
            });
        }
    };
}

// DropTargetDirective.js
var DropTarget= function () {

    return {
        restrict: "A",
        link: function (scope, element, attributes, ctlr) {

            element.bind("dragover", function(eventObject){
                eventObject.preventDefault();
            });

            element.bind("drop", function(eventObject) {

                // invoke controller/scope move method
                scope.moveToBox(parseInt(eventObject.originalEvent.dataTransfer.getData("text")));

                // cancel actual UI element from dropping, since the angular will recreate a the UI element
                eventObject.preventDefault();
            });
        }
    };
}
answer Feb 4, 2017 by Pramod Huilgol
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...