使用angularjs选择文件
我创建了以下指令来处理文件选择器。它工作得很好。但是,我希望将控制器中的所有功能$scope.readFile()和$scope.delete()函数移动到指令本身内部进行处理。在控制器中,我只想从指令中访问attachments[]数组。如何实现这一点 HTML使用angularjs选择文件,angularjs,file,angular-directive,file-type,Angularjs,File,Angular Directive,File Type,我创建了以下指令来处理文件选择器。它工作得很好。但是,我希望将控制器中的所有功能$scope.readFile()和$scope.delete()函数移动到指令本身内部进行处理。在控制器中,我只想从指令中访问attachments[]数组。如何实现这一点 HTML <table class="tblattach" style="width: 100%"> <tr data-ng-repeat="a in attachments">
<table class="tblattach" style="width: 100%">
<tr data-ng-repeat="a in attachments">
<td>
<label class="file-label">Select File<input type="file" id="file" class="file-selector" file-selector="readFile(position, file);" index="$index" /></label>
<input type="text" class="tBox" data-ng-model="a.FileName" title="{{a.FileName}}" readonly />
<img style="vertical-align: middle;" data-ng-click="delete($index);" src="images/cancel.png" height="18" width="18" alt="Delete attachment" title="Delete current attachment" />
</td>
</tr>
</table>
控制器
$scope.readFile = function (position, file) {
var reader = new FileReader();
reader.onloadend = function (e) {
$scope.$apply(function () {
$scope.attachments[position].FileName = file.name;
$scope.attachments[position].FileData = e.target.result;
if ($scope.attachments.length == (position + 1))
$scope.attachments.push({});
});
}
reader.readAsDataURL(file);
};
$scope.delete = function (index) {
if ($scope.attachments.length > 1)
$scope.attachments.splice(index, 1);
else
$scope.attachments[0] = {};
}
你做过这个吗?如果是这样,请将其作为答案发布,并接受eh答案(2天后),因为这将有助于其他人。您是否曾经处理过此问题?如果是这样,请将其作为答案发布,并接受eh答案(2天后),因为这将有助于其他人
$scope.readFile = function (position, file) {
var reader = new FileReader();
reader.onloadend = function (e) {
$scope.$apply(function () {
$scope.attachments[position].FileName = file.name;
$scope.attachments[position].FileData = e.target.result;
if ($scope.attachments.length == (position + 1))
$scope.attachments.push({});
});
}
reader.readAsDataURL(file);
};
$scope.delete = function (index) {
if ($scope.attachments.length > 1)
$scope.attachments.splice(index, 1);
else
$scope.attachments[0] = {};
}