1

I was trying to convert a file to byte array using 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. file conversion done, file added to $scope.FileAttachments also done, but ng-repeat not working at right time. interesting issue is that, when any other event happened inside the view, ng-repeat working nicely.

HTML Code

<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

$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);
     });
}
3
  • You're adding new elements to $scope.FileAttachments outside the scope of your controller, you'll have to call $scope.$apply() to update the scope. Commented May 9, 2017 at 6:04
  • Thanks @Titus you made my day, just working fine. put your answer with code i'll mark it as helpful answer. Commented May 9, 2017 at 6:09
  • Possible duplicate of Angular JS views not updating properly Commented May 9, 2017 at 6:13

1 Answer 1

0

Yes, got it, need to use $scope.$apply() when outside the scope of your controller.

$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);
      });
 }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.