angularjsHow do I implement drag and drop functionality using AngularJS?
To implement drag and drop functionality using AngularJS, you can use the HTML5 Drag and Drop API. The following code example demonstrates how to use the API for drag and drop functionality in AngularJS:
<div ng-app="myApp" ng-controller="myCtrl">
<div ng-drag="true" ng-drag-data="myData">Drag Me</div>
<div ng-drop="true" ng-drop-success="onDropComplete($data,$event)">Drop Here</div>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.myData = {data: 'myData'};
$scope.onDropComplete = function (data,evt) {
console.log("drop success, data:", data);
}
});
</script>
This code example will create two div elements, one with the ng-drag directive and one with the ng-drop directive. When the ng-drag element is dragged and dropped onto the ng-drop element, the onDropComplete function will be called with the data from the ng-drag element and the event object as parameters.
Code explanation
ng-app="myApp": This directive defines the AngularJS application.ng-controller="myCtrl": This directive defines the controller for the application.ng-drag="true"andng-drop="true": These directives enable the drag and drop functionality.ng-drag-data="myData": This directive sets the data to be transferred when the element is dragged.ng-drop-success="onDropComplete($data,$event)": This directive sets the function to be called when the element is dropped.
For more information on implementing drag and drop functionality using AngularJS, see the AngularJS Drag and Drop tutorial.
More of Angularjs
- How can I use AngularJS to transform XLTS files?
- How do I use AngularJS to watch for changes in a variable?
- How can I use AngularJS to construct an XSS payload?
- How do I use the window.open function with AngularJS?
- How do I create a link in AngularJS?
- How can I add a PDF viewer to my AngularJS application?
- How can I use AngularJS to watch for changes in my data?
- How do I use the AngularJS Wiki to find information about software development?
- How can I use AngularJS UI Router to create an application with multiple views?
- How do I use AngularJS to select an item from a list?
See more codes...