Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic draggable with AngularJS?

Tags:

I don't want draggable sortable elements or anything fancy, just a draggable element, like a normal jQuery div draggable object:

$("#draggable").draggable(); 

What is the proper way to do this through Angular? Do I still use jQueryUI, or is there anything in AngularUI I can use? I looked through both Angular libraries but didn't find anything specifically addressing draggable objects.

like image 937
Organiccat Avatar asked Jan 29 '13 18:01

Organiccat


People also ask

How do you make a draggable element?

To make an object draggable set draggable=true on that element. Just about anything can be drag-enabled: images, files, links, files, or any markup on your page. Our example creates an interface to rearrange columns that have been laid out with CSS Grid.

What is the syntax of the draggable function?

The draggable (option) method specifies that an HTML element can be moved in the HTML page. Here, the option parameter specifies the behavior of the elements involved. Syntax: $(selector, context).

How do you get the position of the draggable element?

You can use the drag event: $('#dragThis'). draggable({ drag: function() { var offset = $(this). offset(); var xPos = offset.


1 Answers

Use a directive.

Example:

angular.module("myApp").directive('andyDraggable', function() {   return {     restrict: 'A',     link: function(scope, elm, attrs) {       var options = scope.$eval(attrs.andyDraggable); //allow options to be passed in       elm.draggable(options);     }   }; }); 

HTML

<div andy-draggable>Drag me!</div> <div andy-draggable="{key: value}">Drag me with options!</div> 

Documentation on directives: http://docs.angularjs.org/guide/directive

You could also create data-binding for the element's current position during the drag, hook up events, etc. But this is a really basic version.

like image 148
Andrew Joslin Avatar answered Sep 19 '22 14:09

Andrew Joslin