我想知道如何显示图像列表(通过拖放)。我已经有了拖放部分的指令。 但是,在我的应用程序中,我“解析”FileList以及如何将数据从指令传递给部分?这是工作ob控制器吗?AngularJS:如何显示图像列表(通过拖放)
/**
* Adds drag&drop functionality to a div
* @Example: <div drop-target ng-class="{active:isHot}" class="dropzone">Drop here</div>
*/
angular.module('myApp.directives').
directive('dropTarget', function() {
return function ($scope, $element) {
// Drophandler passes a fileList to the controller
$element.bind('drop', function (evt) {
evt.stopPropagation();
evt.preventDefault();
// FileList should go to the controller/view
var fileList = evt.dataTransfer.files;
console.log(fileList);
$scope.$apply(function() {
$scope.imageList = fileList;
});
return false;
});
// DragOverHandler highlights the dropzone
$element.bind('dragover', function (evt) {
evt.stopPropagation();
evt.preventDefault();
$scope.isHot = true;
return evt.dataTransfer.dropEffect = "copy";
});
// DragOverHandler de-highlights the dropzone
$element.bind('dragleave', function (evt) {
evt.stopPropagation();
evt.preventDefault();
$scope.isHot = false;
});
};
})
您能否让我们知道上面代码中的哪个变量包含已被拖放的文件列表?它是'fileList'吗? – callmekatootie
是的,它是fileList – fabian
那你为什么不把它绑定到'$ scope'?这是一个局部变量... – callmekatootie