I tried to do the same and came up with the following solution:
angular.directive("my:sortable", function(expression, compiledElement){ return function(linkElement){ var scope = this; linkElement.sortable( { placeholder: "ui-state-highlight", opacity: 0.8, update: function(event, ui) { var model = scope.$tryEval(expression); var newModel = []; var items = []; linkElement.children().each(function() { var item = $(this);
Used as follows:
<ol id="todoList" my:sortable="todos" my:onsort="onSort()">
It seems to work quite well. The trick is to undo the DOM manipulation done by sorting before updating the model, otherwise agular will desynchronize with the DOM.
Notification of changes works through the expression my: onsort, which can call controller methods.
I created a JsFiddle based on the angular todo tutorial to show how it works: http://jsfiddle.net/M8YnR/180/
Manuel Woelker Jan 24 2018-12-21T00: 00Z
source share