我正在尝试在angularjs中实现拖放系统。
我希望在拖动开始时克隆被拖动的对象。但是我不知道如何在angularjs中克隆元素及其作用域和链接的 Controller ?
有什么建议么?
最佳答案
不建议使用Angular来克隆DOM元素,因为通常是通过拖放操作完成的。而是,克隆您的对象模型。
假设您以<UL>
显示项目,并且只有在拖动时才显示另一个被拖动的项目:
<ul>
<li ng-repeat="item in items" class="{{item.shadow}}">{{item.text}}</li>
<ul>
<div ng-show="draggedItem != null">{{draggedItem.text}}</div>
并在 Controller 中复制该项目以将其拖到draggedItem中:
$scope.items = [{text:"First"}, {text:"Second"}];
$scope.shadowItem = null; // Item at the original position
$scope.draggedItem = null; // Clone item being moved
$scope.dragStart = function(item) {
$scope.shadowItem = item;
$scope.draggedItem = angular.copy(item);
item.shadow = "shadow"; // set a CSS class to change its look
// From now on, the DIV is dragged around
}
$scope.drop = function() {
// Save the new item position
$scope.draggedItem = null; // Makes the dragged clone item disappear
$scope.shadowItem.shadow = ""; // give the item its normal look back
}
关于angularjs - 在angularjs中克隆HTML元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15436877/