<div jq-sortable="selectedList"> <div ng-repeat="item in selectedList"> <img ng-src="{{item.src}}" /> </div> </div>
JavaScript
app.directive('jqSortable', ['$parse', function($parse) { return function(scope, element, attrs) { /*解析並取得表達式*/ var expr = $parse(attrs['jqSortable']); var $oldChildren; element.sortable({ opacity: 0.7, scroll: false, tolerance: "pointer", start: function() { /*紀錄移動前的 children 順序*/ $oldChildren = element.children('[ng-repeat]'); }, update: function(){ var newList = []; var oldList = expr(scope); var $children = element.children('[ng-repeat]'); /*產生新順序的陣列*/ $oldChildren.each(function(i){ var index = $children.index(this); if(index == -1){ return; } newList[index] = oldList[i]; }); /*將新順序的陣列寫回 scope 變數*/ expr.assign(scope, newList); /*通知 scope 有異動發生*/ scope.$digest(); } }); /*在 destroy 時解除 Sortable*/ scope.$on('$destroy', function(){ element.sortable('destroy'); }); }; }]);
0 回應:
張貼留言