我遇到了一個奇怪的情況。我需要有兩個可排序的列表,這些列表應該通過拖放或添加/刪除事件來交換元素。AngularJS指令共享範圍與ng重複 - 行爲不端
我創建了一個很好的指令。控制器事件也做正確的工作。問題在方法合併時開始(按鈕Add +拖放+按鈕再次添加)。 KA-BOOM!
我放在一起這樣plnkr:http://plnkr.co/edit/DumufP1kDdkz1INAXwmF?p=preview
點擊元素之前單擊按鈕操作(添加/刪除)。
讓我分享一些指令的代碼只是爲了好玩,但請訪問鏈接以查看整個實現。還有就是如何重現在plnkr
.directive('sortableList', function ($log) {
return {
restrict: 'A',
scope: {
fromList: '=',
toList: '='
},
link: function (scope, elm, attrs) {
var callback = {
receive: function (event, ui) {
//-- Get the scope of the list-item
var scopeItem = ui.item.scope();
//-- Get new list index
var newIdx = ui.item.index();
//-- Find position in the list
var prevIdx = scope.fromList.indexOf(scopeItem.obj);
//-- Remove from source list
scope.fromList.splice(prevIdx, 1);
//-- Add to target list
if (newIdx >= scope.toList.length) {
scope.toList.push(scopeItem.obj);
}
else {
scope.toList.splice(newIdx, 0, scopeItem.obj);
}
//ui.item.removeClass('selectedSortListItem').addClass('sortListItem');
scope.$apply();
},
stop: function (event, ui) {
//$log.log(ui);
}
};
//-- Apply jquery ui sortable plug-in to element
elm.sortable({
handle: ".handle",
connectWith: '.sortColumnsConnect',
dropOnEmpty: true,
cancel: ".ui-state-disabled",
receive: callback.receive,
stop: callback.stop
}).disableSelection();
//-- Sniff for list changes
/*scope.$watch(attrs.sortableList, function (newVal) {
//-- Apply callback
//if (angular.isUndefined(newVal)) return;
elm.sortable('option', 'receive', callback.receive);
if (!angular.isUndefined(attrs.trackSorting) && Boolean(attrs.trackSorting)) {
elm.sortable('option', 'stop', callback.stop);
}
});*/
}
}
})
說明問題認識的更多信息。