AngularJS cannot pass $ index from modal loading window

I bang my head on the wall here. I use ng-repeat to populate the table. Inside each line there are two buttons, one for updating the contents of the line and for downloading files. The "Download" button opens a modal download window, in which the user selects files and clicks the "Send" button.

The submit button uses ng-click to launch a function that uses the $ index as parameter. But the value of $ index is always the same no matter which row is selected.

I do not understand that I use the same syntax (albeit outside the modal window) on my update button, which works fine.

HTML:

<tr ng-repeat="item in items | filter:search " ng-class="{'selected':$index == selectedRow}" ng-click="setClickedRow($index)">
   <td>{{$index}}</td>
   <td ng-hide="idHidden" ng-bind="item.Id"></td>
   <td ng-hide="titleHidden">
      <span  data-ng-hide="editMode">{{item.Title}}</span>
      <input  type="text" data-ng-show="editMode" data-ng-model="item.Title" data-ng-required />
   <td> 
      <button type="button" class="btn btn-primary uploadBtn" data-ng-show="editMode" data-toggle="modal" data-target="#uploadModal">Upload file  <i class="fa fa-cloud-upload"></i></button>
      <!-- Upload Modal -->
      <div class="modal fade" id="uploadModal" tabindex="-1" role="dialog" aria-labelledby="uploadModalLabel" aria-hidden="true">
         <div class="modal-dialog">
            <div class="modal-content">
               <div class="modal-header">
                  <h3 class="modal-title" id="uploadModalLabel">Options</h3>
               </div>
               <div class="modal-body">
                  <h4>Upload Documents</h4>
                  <form>
                     <div class="form-group">
                        <select  data-ng-model="type" class="form-control" id="fileTypeSelect">
                           <option value="Policy">Policy</option>
                           <option value="SOP">SOP</option>
                        </select>
                        <br>
                        <div class="input-group"> <span class="input-group-btn">
                           <input type="file" id="file">
                           </span>
                        </div>
                        <br>
                        <button  type="button" class="btn btn-default" data-ng-click="uploadAttachment($index, type)">Upload</button>
                  </form>
                  </div>
                  <div class="modal-footer">
                     <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                  </div>
               </div>
            </div>
         </div>
      </div>
      <button type="button" data-ng-hide="editMode" data-ng-click="editMode = true;" class="btn btn-default pull-right">Edit <i class="fa fa-pencil-square-o"></i></button>
      <button type="button" data-ng-show="editMode" data-ng-click="editMode = false; updateItem($index)" class="btn btn-default">Save</button>
      <button type="button" data-ng-show="editMode" data-ng-click="editMode = false; cancel()" class="btn btn-default">Cancel</button>
      </td>`

JS:

$scope.uploadAttachment = function executeUploadAttachment(index, type) {
var listname = "Risk Register";
var id = $scope.items[index].Id;
console.log(indexID);
readFile("uploadControlId").done(function(buffer, fileName) {
    uploadAttachment(type, id, listname, fileName, buffer).done(function() {
        alert("success");
    }).fail(function() {
        alert("error in uploading attachment");
    })
}).fail(function(err) {
    alert("error in reading file content");
});
}

, uploadAttachment($index, type), ng-click, . , , .

, . .

, ?

Edit:

DonJuwe.

:

$scope.openModal = function(index) {
var modalInstance = $modal.open({
    templateUrl: 'www.test.xxx/App/uploadModal.html',
    controller: 'riskListCtrl',
    resolve: {
        index: function() {
            return index;
        }
    }
});
};

:

 <div class="modal-header">
   <h3 class="modal-title" id="uploadModalLabel">Options</h3>
</div>
<div class="modal-body">
   <h4>Upload Documents</h4>
   <form>
      <div class="form-group">
         <select  data-ng-model="type" class="form-control" id="fileTypeSelect">
            <option value="Policy">Policy</option>
            <option value="SOP">SOP</option>
         </select>
         <br>
         <div class="input-group"> <span class="input-group-btn">
            <input type="file" id="file">
            </span>
         </div>
         <br>
         <button  type="button" class="btn btn-default" data-ng-click="uploadAttachment($index, type)">Upload</button>
   </form>
   </div>
      <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
   </div>
</div>

, , , RiskListCtrl ( , ):

  $scope.uploadAttachment = function executeUploadAttachment(index, type) {
var listname = "Risk Register";
var id = $scope.items[index].Id;
console.log(indexID);
readFile("uploadControlId").done(function(buffer, fileName) {
    uploadAttachment(type, id, listname, fileName, buffer).done(function() {
        alert("success");
    }).fail(function() {
        alert("error in uploading attachment");
    })
}).fail(function(err) {
    alert("error in reading file content");
});
}

, $scope.items[index].Id . : Cannot read property 'Id' of undefined

+4
2

. , , . resolve open(options).

, . , id ( , ). $index:

<button type="button" class="btn btn-primary uploadBtn" data-ng-show="editMode" ng-click="openModal($index)">Upload file  <i class="fa fa-cloud-upload"></i></button>

:

$scope.openModal = function(index) {
    var modalInstance = $modal.open({
        templateUrl: 'myPath/myTemplate.html',
        controller: 'MyModalCtrl',
        resolve: {
            index: function() {
                return index;
            }
        }
    });
};

index MyModalCtrl, index:

angular.module('myModule', []).controller('MyModalCtrl', function($scope, index) {
    $scope.index = index;
});
+3

, ng-click, , , , make . :

     <button type="button" data-ng-show="editMode" data-ng-click="editMode = false; updateItem($index)" class="btn btn-default">Save</button>

$scope.updateItem = functon(index)
{
$scope.tempVar = index;
}

now use the value of tempVar in you function 

 $scope.uploadAttachment = function executeUploadAttachment(index, type) {
var index = tempVar;            //assign the value of tempvar to index
var listname = "Risk Register";
var id = $scope.items[index].Id;
console.log(indexID);
readFile("uploadControlId").done(function(buffer, fileName) {
    uploadAttachment(type, id, listname, fileName, buffer).done(function() {
        alert("success");
    }).fail(function() {
        alert("error in uploading attachment");
    })
}).fail(function(err) {
    alert("error in reading file content");
});
}
+1

All Articles