简体   繁体   中英

How to get index inside ng-repeat with file input?

I have an array of records which I upload using file input. My purpose is to upload a file in a row when '+' is clicked and remove the file when 'x' is clicked.

My issue is, whenever I upload one file in one record, all other records are also get updated by the same file. I'm sure that I'm missing to use the $index logic here. I'm already passing this with onchange event of file input. How can I pass $index along with that?

<div ng-repeat="itemList in FilesToShow track by $index">
    <div style="width: 90%; display: inline-block;">{{itemList.FileName}}</div>
    <div style="cursor:pointer;display: inline-block;"
         ng-click="removefile(itemList)">x</div>
    <div style="cursor:pointer;display: inline-block;"
         ng-focus="$parent.focused = true" ng-if="FilesToShow.length==0">+</div>
</div>
<input ng-if="focused" ng-disabled="FilesToShow.length==1" type="file"
       id="fileId" value="Browse..." name="file"
       onchange="angular.element(this).scope().getFileDetails(this)" />
$scope.FilesToShow = [];
$scope.getFileDetails = function(hisLstFile) {
  $scope.$apply(function() {
    var Historyvalue = {
      FileName: hisLstFile.files[0].name,
      IsFileDeleted: false,
      DownloadPath: null,
      Id: null
    };
    $scope.FilesToShow.push(Historyvalue);
  });
}

You can pass the $index as an argument in the function. Just make sure that it's in the ng-repeat scope. Like:

onchange="myFunction(this, $index)";

good luck :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM