3

html:

<div ng-if="showSubHeader" class="topPull" draggable >
            <button class="button-icon" on-swipe-down="changeSlide()"><img src="img/topImg.jpg"></button>
                           </div>

directive:

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

        el.addEventListener(
            'dragstart',
            function(e) {
                e.originalEvent.dataTransfer.effectAllowed = 'move';
               // e.dataTransfer.setData('Text', this.id);
                this.classList.add('drag');
                return false;
            },
            false
        );

        el.addEventListener(
            'dragend',
            function(e) {
                this.classList.remove('drag');
                return false;
            },
            false
        );
    }
})

Error:

Uncaught TypeError: Cannot read property 'dataTransfer' of undefined(anonymous function) @ controllers.js:12
app.js:19 Uncaught TypeError: Cannot set property 'effectAllowed' of undefined

How do I fix this error?

I followed this post http://blog.parkji.co.uk/2013/08/11/native-drag-and-drop-in-angularjs.html and need only the draggable directive to work.

ci_
  • 8,594
  • 10
  • 39
  • 63
Simran Kaur
  • 1,103
  • 5
  • 24
  • 40
  • It;s possible your broswer isn't compatible with `e.originalEvent` (where you can simply use the native window event), and hence being `undefined` [see this answer](http://stackoverflow.com/a/16675056/3149020). – Spencer Wieczorek Sep 08 '15 at 12:16

1 Answers1

8

Just change the

 e.originalEvent.dataTransfer.effectAllowed = 'move';

with

e.dataTransfer.effectAllowed = 'move';

Demo JSfiddle: http://jsfiddle.net/xvh9L6do/2/

According to : http://blog.parkji.co.uk/2013/08/11/native-drag-and-drop-in-angularjs.html

Mohit Pandey
  • 3,679
  • 7
  • 26
  • 38
Juned Lanja
  • 1,466
  • 10
  • 21
  • It gives error in my app this is exactly why I had added originalEvent and yet it did not work. – Simran Kaur Sep 08 '15 at 12:43
  • 1
    Its working fine on their codepen.You have changed e.dataTransfer.effectAllowed = 'move'; to e.originalEvent.dataTransfer.effectAllowed = 'move'; so its giving error.Please see http://codepen.io/parkji/pen/JtDro – Juned Lanja Sep 08 '15 at 12:51