Ng-repeat does not update with array changes

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.

wrong configuration

, , , . , $timeout $apply() , .

, DOM, , () , DOM (). .

Update:

<pre ng-bind="instructionsObj.instructions|json</pre>, , . .

+4
1

, , , .

JS:

sortableEle = $('.sort').sortable({
    start: $scope.dragStart,
    update: $scope.dragEnd
});

$scope.dragStart = function(e, ui) {
    ui.item.data('start', ui.item.index());
}
$scope.dragEnd = function(e, ui) {
    var start = ui.item.data('start'),
        end = ui.item.index();

    $scope.instructionsObj.instructions.splice(end, 0,
        $scope.instructionsObj.instructions.splice(start, 1)[0]);
    currentRecipe.setInstructions($scope.instructionsObj.instructions);
    $scope.$apply();
    sortableEle.refresh();
}

$scope.$apply() , sortableEle.refresh() .

, , 10% , .

FYI: , , .

0

All Articles