Note that there are some explanatory texts on larger screens.

plurals
  1. POangular.bind() for 'drop' event listener not behaving properly
    text
    copied!<p><a href="http://plnkr.co/edit/Cvms35UyKOV1iwUmvLqN" rel="nofollow noreferrer">Plunker demonstrating code</a></p> <p>I have a <code>&lt;div&gt;</code> element with the directive <code>dragAndDrop</code> set as an attribute:</p> <pre><code>&lt;div ng-class="divClass" drag-and-drop&gt;Drop files here&lt;/div&gt; </code></pre> <p><em>(as a side, and less important note, ng-class is not behaving as expected, but that's a <a href="https://stackoverflow.com/questions/15419839/angularjs-directive-scope-not-setting-ng-class-on-element">separate issue</a>)</em> What I would like is for users to drag and drop files from their desktop to the div. Upon doing so though, I use <code>angular.bind()</code> to detect the drop event. Of course once they drop, I call <code>e.stopPropagation()</code> and <code>e.preventDefault()</code>, but the page continues on with redirecting the page regardless. What am I doing wrong that would prevent the two aforementioned methods from executing?</p> <p><code>dragAndDrop</code> directive:</p> <pre><code>directive('dragAndDrop', function() { return { restrict: 'A', link: function($scope, elem, attr) { elem.bind('dragenter', function(e) { e.stopPropagation(); e.preventDefault(); // still can't get this one to behave: // https://stackoverflow.com/q/15419839/740318 $scope.divClass = 'on-drag-enter'; }); elem.bind('dragleave', function(e) { e.stopPropagation(); e.preventDefault(); $scope.divClass = ''; }); elem.bind('drop', function(e) { var droppedFiles = e.dataTransfer.files; // It's as though the following two methods never occur e.stopPropagation(); e.preventDefault(); if (droppedFiles.length &gt; 0) { for (var i=0,ii=droppedFiles.length;i&lt;ii;i++) { $scope.files.push(droppedFiles[i]); } } }); } }; }); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload