Georgi Naumov
Georgi Naumov

Reputation: 4201

Drop event is not called inside AngularJS directive

I have one strange problem with drop event into AngularJS directive. Listener is added because console show according DOM element - <td class="ng-isolate-scope" droppable="" colspan="2">. When I change to other event - click for example everything working properly. Here is my html code:

<div ng-app="app">
    <div ng-controller="testCtrl">
        <table id="testtable">
            <tr>
                <td colspan="2" droppable>
                    <a data-ng-repeat="firstLink in first" href="#">
                        {{firstLink.text}}
                    </a>
                </td>
            </tr>
            <tr>
                <td>
                    <a data-ng-repeat="secondLink in second" href="#" draggable>
                        {{secondLink.text}}
                    </a>
                </td>    
                <td>
                    <a data-ng-repeat="thirdLink in third" href="#" draggable>
                        {{thirdLink.text}}
                    </a>
                </td>    
            </tr>
        </table>
    </div>
</div> 

and here is me JavaScript:

var app = angular.module('app', []);

app.controller('testCtrl', function ($scope) {
    $scope.first = [
        {text: 1},
        {text: 2},
        {text: 3}
    ];

    $scope.second = [
        {text: 4},
        {text: 5},
        {text: 6}
    ];

    $scope.third =  [
        {text: 7},
        {text: 8},
        {text: 9}        
    ];
});

app.directive('draggable', function () {
    return function(scope, element) {
        // this gives us the native JS object
        var el = element[0];

        el.draggable = true;

        el.addEventListener(
            'dragstart',
            function(e) {
                console.log('dragstart called');
                e.dataTransfer.effectAllowed = 'move';
                e.dataTransfer.setData('Text', 'test test test');
                return false;
            },
            false
        );

        el.addEventListener(
            'dragend',
            function(e) {
                console.log('dragend called');
                e.preventDefault();
                return false;
            },
            false
        );
    }
});


app.directive('droppable', function () {
    return {
        scope: {
            drop: '&' // parent
        },
        link: function(scope, element) {
            var el = element[0];
            console.log(el);
            el.addEventListener(
                'drop',
                function(e) {
                    console.log('Why im not called?');
                    return false;
                },
                false
            );
        }
    }
});

Here is jsFiddle: http://jsfiddle.net/zono/kyFT8/

What causes this behavior?

Best regards.

Upvotes: 4

Views: 2447

Answers (1)

Georgi Naumov
Georgi Naumov

Reputation: 4201

I found the solution. From dotoro web reference:

By default, the target of a drag operation can be an editable element (textarea, input:text, input:password, input:search), an element in content editable mode, a document in design mode or another application. If you want to allow for other elements to be a drop target during a drag operation, cancel both the ondragenter and ondragover events.

So here is working jsfiddle:

http://fiddle.jshell.net/kyFT8/11/

Upvotes: 4

Related Questions