2013-07-27 111 views
1

我是AngularJS的新手。angularJS:帶數據結構的同步重新排序列表

我正在嘗試使用Angular ui-sortable插件重新排列數組。我希望數據數組與html ul的順序相同,但似乎無法成功。

var myapp = angular.module('myapp', ['ui']); 

myapp.controller('controller', function ($scope) { 
    $scope.sortableOptions = { 
     start: function (e, ui) { 
      $scope.oldIndex = ui.item.index(); 
     }, 
     update: function (e, ui) { 
      var newIndex = $scope.newIndex = ui.item.index(); 
      var oldIndex = $scope.oldIndex; 

      $scope.oldArray = $scope.list.join(';'); 

      var item = $scope.list.splice(oldIndex, 1); 
      $scope.list.splice(newIndex, 0, item[0]); 

      $scope.itemMoved = item; 
      $scope.newArray = $scope.list.join(';'); 
     } 
    } 

    $scope.list = ["one", "two", "three", "four", "five", "six"]; 
}); 

angular.bootstrap(document, ['myapp']); 

任何想法?

小提琴: http://jsfiddle.net/DxjXQ/2/

回答

1

你應該等到角更新它的範圍(申請)

所以可能的解決方案正好被設置到隊列

$timeout(function(){ 
      $scope.newArray = $scope.list.join(';'); 
      }, 0); 

http://jsfiddle.net/DxjXQ/5/

結束