[英]Angular ng-file-upload delete
Trying to figure best approach to be able to delete a file when using ng-file-upload. 尝试在使用ng-file-upload时找出能够删除文件的最佳方法。 I got my list of files with a list.
我得到了带有列表的文件列表。 My html is:
我的HTML是:
<div>
<p>Files:</p>
</div>
<div>
<ul>
<li ng-repeat="f in files" style="font:smaller">{{f.name}} {{f.$error}} {{f.$errorParam}}</li>
</ul>
</div>
In my controller, I have: 在我的控制器中,我有:
$scope.$watch('files', function () {
$scope.upload($scope.files);
});
$scope.$watch('file', function () {
if ($scope.file != null) {
$scope.files = [$scope.file];
}
});
$scope.log = '';
$scope.upload = function (files) {
if (files && files.length) {
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (!file.$error) {
Upload.upload({
url: (serviceBase + 'api/ppt/docs/upload/multi?transId=' + [$scope.transId]),
data: {
username: $scope.username,
file: file,
filename: file.name
}
}).progress(function (evt) {
var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
$scope.log = 'progress: ' + progressPercentage + '% ' +
evt.config.data.file.name + '\n' + $scope.log;
}).success(function (data, status, headers, config) {
$timeout(function() {
$scope.log = 'file: ' + config.data.file.name + ', Response: ' + JSON.stringify(data) + '\n' + $scope.log;
});
}).error(function(data, status, headers, config) {
var errors = [];
$scope.errors = data;
for (var i=0; i<$scope.errors.length; i++)
{
errors.push($scope.errors[i].errMsg);
}
$scope.message = "Error uploading files due to: " + errors.join(' ');
});
}
}
}
};
I see some examples in the documentation, but not seeing best ways to implement, especially if I want individual deletes/remove for each file uploaded rather than just all. 我在文档中看到了一些示例,但没有看到实现的最佳方法,特别是如果我希望对每个上传的文件进行单独的删除/删除,而不仅仅是所有文件。
Thanks much. 非常感谢。
I suppose the straightforward approach will do here. 我想这个直截了当的做法会在这里做。
deleteFile
handler to your scope taking array index of the file being deleted. deleteFile
处理程序添加到作用域,获取要删除的文件的数组索引。 ng-click="deleteFile($index)
. $index
is created by ng-repeat
. ng-click="deleteFile($index)
。 $index
由ng-repeat
创建。 files
array to remove the deleted entry. files
数组以删除已删除的条目。 HTML: HTML:
<li ng-repeat="f in files">
{{f.name}} {{f.$error}} {{f.$errorParam}} <a class="deleteHandle" ng-click="deleteFile($index)">×</a>
</li>
JS: JS:
$scope.deleteFile = function(idx) {
var file = $scope.files[idx];
if (file.isUploaded) {
$http.delete('/api/files/'+file.id).then(function(response){
if (response.status == 200) {
$scope.files.splice(idx, 1);
}
});
} else {
$scope.files.splice(idx, 1);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.