我有两个指令,可拖动和可放置。指令本身将 jquery ui 函数应用于元素。我的可拖动项目位于一个控制器中,该控制器具有一个带有可放置 div 的父控制器。如何将项目从子控制器项目列表中删除到父控制器列表。还从屏幕上的可拖动列表中删除该项目并增加可放置 div 的长度。
我已经设置了一个jsfiddle,
HTML
<body ng-app="myApp">
<div ng-controller="parentCtrl">
<div id="dropzone" data-index="1" droppable>
{{dropped_items.length}}
</div>
<div ng-controller="childCtrl">
<div ng-repeat="item in items"
data-index="{{$index}}" class="note" draggable>
{{item.title}}
</div>
</div>
</div>
</body>
Javascript
var app = angular.module('myApp', []);
app.controller('parentCtrl', function ($scope){
var dropped = [
{id:1, title:'Note 1'},
{id:2, title:'Note 2'},
{id:3, title:'Note 3'}
];
$scope.dropped_items = dropped;
});
app.controller('childCtrl', function ($scope){
var data = [
{id:4, title:'Note 4'},
{id:5, title:'Note 5'},
{id:6, title:'Note 6'},
{id:7, title:'Note 7'},
{id:8, title:'Note 8'}
];
$scope.items = data;
});
app.directive('draggable', function() {
return {
restrict:'A',
link: function(scope, element, attrs) {
element.draggable({
revert:true
});
}
};
});
app.directive('droppable', function($compile) {
return {
restrict: 'A',
link: function(scope,element,attrs){
element.droppable({
drop:function(event,ui) {
var dragIndex = angular.element(ui.draggable).data('index');
var dropIndex = angular.element(this).data('index');
console.log(dragIndex);
console.log(dropIndex);
console.log(scope);
scope.$apply();
}
});
}
};
});