我正在尝试在 angularjs 中实现拖放系统。
我希望在拖动开始时克隆拖动的对象。但是我不知道如何在 angularjs 中克隆元素及其范围和链接控制器?
有什么建议么?
不建议使用 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>
并在控制器中,创建项目的副本以拖动到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
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)