Clone html element in angularjs

angularjs

Solution

Cloning DOM elements as it is usually done for drag & drop is not recommended with Angular. Instead, clone your object model.

Let's say you display items in an `<UL>` and have another dragged item visible only while dragging:

<ul>
    <li ng-repeat="item in items" class="{{item.shadow}}">{{item.text}}</li>
<ul>
<div ng-show="draggedItem != null">{{draggedItem.text}}</div>

and in the controller, make a copy of the item to drag into 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
}

Problem

I'm trying to implement drag and drop system in angularjs. I want dragged object to be cloned on drag start. However I have no idea how to clone an element along with it's scope and linked controller in angularjs? Any suggestions?

Original source