2015-05-29 37 views
4

我在这里撞墙。我正在使用ng-repeat来填充表格。每行内部我有2个按钮,一个用于更新行内容和上传文件。上传按钮打开引导模式窗口,用户选择文件并点击提交。

提交按钮使用ng-click运行一个使用$ index作为参数的函数。但是无论选择哪一行,$ index值总是相同的。

我不明白的是我在我的更新按钮上使用了完全相同的语法(虽然在模式窗口之外),这很好。

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"); 
}); 
} 

所以这是由NG-点击触发的功能uploadAttachment($index, type)没有通过正确的索引号。它总是通过相同的,不管它被点击的行是什么。

我省略了一些无关的代码。如果需要,我可以提供整个事情。

对我失踪的任何建议?

编辑:

我试图实现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

+0

'​​{{$指数}}'总是一样的呢? – DonJuwe

+0

Nope'​​{{$ index}}'显示正确的数字。 – TietjeDK

+0

而你所指的这条线'$ index'是相同的吗? '上传' – DonJuwe

回答

3

模态窗口有其自己的范围。这意味着你需要解决数据你想传递到模态的范围。为此,请在模式open(options)方法中使用resolve

在我给你举个例子之前,我想建议你的所有表项只有一个模态。这样可以让您保留一个可以轻松使用的模板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; 
      } 
     } 
    }); 
}; 

现在,您可以通过访问indexMyModalCtrl的范围注入index

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

我不确定我是否正确使用它。请参阅我的编辑。 – TietjeDK

+1

@TietjeDK我为你创建了一个小提琴:http://jsfiddle.net/hkLugtz2/ – DonJuwe

+0

谢谢 - 它正在工作!做得好。 – TietjeDK

1

因为您正在获取模型外的索引值,您还可以使用ng-click,然后在你的控制器中调用一个函数,并将索引值存储在一个临时变量中,然后当你使用submit按钮时,只需要创建另一个变量并将临时变量的值赋给变量。例如:

 <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"); 
}); 
}