我想知道如何顯示圖像列表(通過拖放)。我已經有了拖放部分的指令。 但是,在我的應用程序中,我「解析」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