I looked at this problem for many hours and read many other questions that seem to be exactly the same, and I tried all of their solutions, but no one works.
I have an array of objects ( instructionObj.instructions), and these objects are repeated using ng-repeat, and their template is a directive.
<div instruction-directive ng-repeat="i in instructionsObj.instructions"> </div>
Then I enable the sorting of elements using the jQuery UI sortable.
var first, second;
$( ".sort" ).sortable({
axis: "y",
start: function( event, ui ) {
first = ui.item.index();
},
stop: function( event, ui ) {
second = ui.item.index();
console.log(first + " " + second);
rearrange(first, second);
$scope.$apply();
}
});
Then I access the index of the beginning and end of the moved object and change the array accordingly:
function rearrange(f, s){
$timeout(function () {
$scope.instructionsObj.instructions.splice(s, 0,
$scope.instructionsObj.instructions.splice(f, 1)[0]);
$scope.$apply();
});
}
All this works great most of the time. One of the scenarios that I detected a failure is a reordering of objects as such (one column is the current position of all objects displayed on the screen):
a | b | c | d | and
b | c | d | c | b
c | d | b | b | d
d | a | a | a | with
a, d, c, b. a, b, d, c.

, , , . , $timeout $apply() , .
, DOM, , () , DOM (). .
Update:
<pre ng-bind="instructionsObj.instructions|json</pre>, , . .