简体   繁体   English

Angular $ scope与文件输入类型冲突

[英]Angular $scope conflict with file input type

I was trying to convert a file to byte array using angularjs. 我试图使用angularjs将文件转换为字节数组。 its working fine, also add the byte code and filename to an array($scope.FileAttachments), and when it'll be added to $scope.FileAttachments client side ng-repet will display the attached file. 它的工作正常,还将字节代码和文件名添加到数组($ scope.FileAttachments),当它被添加到$ scope.FileAttachments时,客户端ng-repet将显示附加文件。 file conversion done, file added to $scope.FileAttachments also done, but ng-repeat not working at right time. 文件转换完成后,文件添加到$ scope.FileAttachments也完成了,但是ng-repeat在正确的时间没有工作。 interesting issue is that, when any other event happened inside the view, ng-repeat working nicely. 有趣的问题是,当视图内部发生任何其他事件时,ng-repeat工作得很好。

HTML Code 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 Controller's Code 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);
     });
}

Yes, got it, need to use $scope.$apply() when outside the scope of your controller. 是的,得到它,需要使用$ 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);
      });
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular如何设置输入类型文件的样式 - Angular how to style input type file Javascript(Jquery)输入类型提交或按钮冲突 - Javascript(Jquery) input type submit or button conflict 如何使用controllerAs(与$ scope相对)-实际上是Swig / Angular冲突 - How to use controllerAs (as opposed to $scope) - Actually a Swig/Angular conflict 角度输入[type =“ checkbox”]虽然在范围内为真,但未在视图中进行检查 - Angular input[type=“checkbox”] doesn't getting checked in view though its value is true in scope 扳机<input type="file">以编程方式选择带有角度的文件 - Trigger <input type="file"> file select with angular programatically Angular 2 如何从多个输入类型文件中删除文件? - Angular 2 How to remove file from files of input type file multiple? 用于输入类型文件的反应式上传表单的 Jest 测试用例<input type="file" formControlName="userslist"> (角 7) - Jest Test Case for reactive upload form for input type file <input type="file" formControlName="userslist"> (Angular 7) Angular 8 ReactiveForm 输入类型“文件”无法识别 Edge 上的值 - Angular 8 ReactiveForm Input type "file" doesn't recognize value on Edge 来自输入类型=文件的角度传递参考变量 - Angular pass reference variable from input type=file 如何通过角度2中的函数触发事件单击输入类型=“文件”? - how to trigger event click at input type=“file” by function in angular 2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM