我有一个将指令设置为属性的<div>
元素:dragAndDrop
<div ng-class="divClass" drag-and-drop>Drop files here</div>
(作为一个方面,不太重要的说明,ng-class 的行为不符合预期,但这是一个单独的问题)
我希望用户将文件从他们的桌面拖放到 div。不过,这样做后,我会使用它angular.bind()
来检测 drop 事件。当然,一旦它们掉线,我就会调用e.stopPropagation()
and e.preventDefault()
,但是无论如何页面都会继续重定向页面。我做错了什么会阻止上述两种方法执行?
dragAndDrop
指示:
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 > 0) {
for (var i=0,ii=droppedFiles.length;i<ii;i++) {
$scope.files.push(droppedFiles[i]);
}
}
});
}
};
});