我把头撞在墙上。我使用ng-repeat填充表格。在每行内部,我有2个按钮,一个用于更新行内容和上传文件。上传按钮打开一个引导模式窗口,用户在该窗口中选择文件并单击提交。
提交按钮使用ng-click运行一个使用$ index作为参数的函数。但无论选择哪一行,$ index值始终相同。
我不明白的是,我在更新按钮上使用完全相同的语法(虽然在模态窗口之外),这很好用。
HTML:
FILE=t.txt
for i in baseebna1 ebna1 baseLDL LDL
do
for ((n=1;n<=3;n++))
do
awk -v pattern=_I"$i"_"$n" '$0 ~ pattern && /\|/ {printf "%15s %7.2f %7.2f %7.2f %7.2f\n",$1,$3,$7,$8,$6}' $FILE >> 1.txt
awk -v p1="$i"_cat '$0 ~ p1 && /\|/ {printf "%15s %7.2f %7.2f %7.2f %7.2f\n",$1,$3,$7,$8,$6}' $FILE >> 1.txt
done
done
JS:
<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>`
因此,由ng-click触发的函数$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");
});
}
不会传递正确的索引号。无论点击哪一行,它总是传递相同的。
我省略了一些无关的代码。如果需要,我可以提供整件事。
对我遗失的任何建议?
修改
我试图实施 DonJuwe 建议。
我在控制器中添加了这个:
uploadAttachment($index, type)
这是我的模态模板:
$scope.openModal = function(index) {
var modalInstance = $modal.open({
templateUrl: 'www.test.xxx/App/uploadModal.html',
controller: 'riskListCtrl',
resolve: {
index: function() {
return index;
}
}
});
};
最后我的函数驻留在RiskListCtrl(我使用的唯一控制器)中:
<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>
$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
答案 0 :(得分: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;
}
}
});
};
现在,您可以通过注入index
来访问MyModalCtrl
范围内的index
:
angular.module('myModule', []).controller('MyModalCtrl', function($scope, index) {
$scope.index = index;
});
答案 1 :(得分:1)
因为你得到了模型外的索引值,你也可以使用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");
});
}