我试图使用angularjs将文件转换为字节数组。它工作正常,还将字节代码和文件名添加到数组($ scope.FileAttachments),当它被添加到$ scope.FileAttachments时,客户端ng-repet将显示附加文件。文件转换完成后,文件添加到$ scope.FileAttachments也完成了,但是ng-repeat在正确的时间没有工作。有趣的问题是,当视图内部发生任何其他事件时,ng-repeat工作得很好。
HTML代码
<input id="File1" ng-model="File1" onchange="angular.element(this).scope().file1Upload()" type="file" />
<table class="table table-striped table-bordered table-list">
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody id="tblAttachments">
<tr ng-repeat="item in FileAttachments track by $index">
<td>{{item.AttachmentDescription}}</td>
</tr>
</tbody>
</table>
AngularJS控制器代码
$scope.FileAttachments = [];
var fileData;
function getBuffer(resolve) {
var reader = new FileReader();
reader.readAsArrayBuffer(fileData);
reader.onload = function () {
var arrayBuffer = reader.result
var bytes = new Uint8Array(arrayBuffer);
resolve(bytes);
}
}
$scope.file1Upload=function() {
var files = document.getElementById("File1").files;
fileData = new Blob([files[0]]);
var promise = new Promise(getBuffer);
promise.then(function (data) {
$scope.FileAttachments.push({
"AttachmentDescription": files[0].name.toString(),
"FileValue": data.toString()
});
}).catch(function (err) {
console.log('Error: ', err);
});
}
答案 0 :(得分:0)
是的,得到它,需要在控制器范围之外使用$ scope。$ apply()。
$scope.file1Upload=function() {
var files = document.getElementById("File1").files;
fileData = new Blob([files[0]]);
var promise = new Promise(getBuffer);
promise.then(function (data) {
$scope.$apply(function () {
$scope.FileAttachments.push({
"AttachmentDescription": files[0].name.toString()
"FileValue": data.toString()
});
});
}).catch(function (err) {
console.log('Error: ', err);
});
}