- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用带有 Angular js 的 ASP.NET MVC。
我有一个页面说 hello.cshtml。
其中有一个对话框,其中有按钮“添加”,单击该按钮后,我会在对话框本身具有两列的表中添加新行。
1) 用于从下拉列表中选择值
2) 用于输入值的文本框。
在这里,当我在表中添加新值并选择列表并输入运行完美的值时,只是在编辑操作期间在下拉列表中设置选定值时出现问题。
我正在创建该添加行的值的列表来存储信息,并且在更改下拉列表时我当场存储值,以便运行良好,但在编辑过程中我如何将下拉列表值设置回设置,因为 ng-model是在下拉列表中分配一次的一个!
我有以下代码来使用正确运行的 jquery 在更改 dropdowlist 时设置数组对象中的值:
$scope.changeLocationRack = function (receiptlocationid, index) {
for (var i = 0; i < $scope.ReceiptDetail.ReceiptLocationList.length; i++) {
if ($scope.ReceiptDetail.ReceiptLocationList[i].ReceiptLocationId == receiptlocationid) {
$scope.ReceiptDetail.ReceiptLocationList[i].LocationRackId = $("#ddllocationrack_" + index).val();
$scope.ReceiptDetail.ReceiptLocationList[i].LocationName = $("#ddllocationrack_" + index + " :selected").text();
break;
}
}
}
现在要在编辑功能中设置那些 dropdowlist 值,我应该做什么?添加了下拉列表和新行的 HTML 代码:
<div class="form-group row gutter">
<button class="btn btn-primary pull-right" type="button" style="margin-right: 10px" ng-click="AddLocationRack()">Add Location Rack</button>
</div>
<div class="form-group gutter">
<fieldset>
<legend>
<b>Receipt Location Racks</b>
</legend>
<div class="form-group gutter">
<table id="tblReceiptLocationRack" class="table table-striped table-condensed table-bordered no-margin">
<thead>
<tr style="background-color:#357CBA; color: white;">
<th>Location Rack</th>
<th>Quantity</th>
<th></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="r in ReceiptDetail.ReceiptLocationList track by $index">
<td>
<select class="form-control dropdownSelect2" ng-model="SelectedLocationRack" title="Choose Location Rack from List" ng-options="s as s.RackName for s in EnveeFee.LocationRackList track by s.LocationRackId" ng-change="changeLocationRack(r.ReceiptLocationId,r.ReceiptLocationId)" id="ddllocationrack_{{r.ReceiptLocationId}}" required>
<option value="">Select Location Rack</option>
</select>
</td>
<td>
<input type="number" class="form-control" style="text-align:right" id="qtyreceived{{$index}}" ng-model="r.QtyReceived" required />
</td>
<td class="text-center" style="vertical-align:middle"><span title="Remove" style="font-size:18px;color:red;cursor:pointer;" class="icon-cross2" ng-click="removelocationrack(r.ReceiptLocationId)"></span></td>
</tr>
</tbody>
</table>
</div>
</fieldset>
</div>
Angular 代码:
$scope.ReceiptDetail = {
ReceiptLineId: 0,
ReceiptId: 0,
SizeId: 0,
ReceiptQty: '',
UnitRate: '',
Amount: '',
ReceiptLocationList: [],
cadrate: '',
hst: '',
Size: '',
LocationName: '',
SizeObject: {}
}
$scope.ReceiptLocation = {
ReceiptLocationId: 0,
LocationRackId: 0,
QtyReceived: '',
QtyIssued: '',
LocationName: ''
}
$scope.AddLocationRack = function () {
$scope.ReceiptDetail.ReceiptLocationList.push($scope.ReceiptLocation);
}
最佳答案
首先,您不应该将 $scope
作为对象推送到 ReceiptLocationList
中,因为这将导致唯一的对象处理,其中所有选定的对象属性都将被覆盖。只需将一个唯一的对象插入您的列表即可:
$scope.ReceiptDetail = {
ReceiptLineId: 0,
ReceiptId: 0,
SizeId: 0,
ReceiptQty: '',
UnitRate: '',
Amount: '',
ReceiptLocationList: [],
cadrate: '',
hst: '',
Size: '',
LocationName: '',
SizeObject: {}
};
$scope.AddLocationRack = function () {
$scope.ReceiptDetail.ReceiptLocationList.push({
ReceiptLocationId: 0,
LocationRackId: 0,
QtyReceived: '',
QtyIssued: '',
LocationName: ''
});
};
在 View 中,您需要为选择的 ng-model
属性设置正确的模型属性,例如:
<tr ng-repeat="r in ReceiptDetail.ReceiptLocationList track by $index">
<td>
<select class="form-control dropdownSelect2"
ng-model="r.ReceiptLocationId"
title="Choose Location Rack from List"
ng-options="s.LocationRackId as s.RackName for s in EnveeFee.LocationRackList track by s.LocationRackId"
id="ddllocationrack_{{r.ReceiptLocationId}}" required>
<option value="">Select Location Rack</option>
</select>
</td>
<td>
<input type="number"
class="form-control"
style="text-align:right"
id="qtyreceived{{$index}}"
ng-model="r.QtyReceived" required />
</td>
<td class="text-center"
style="vertical-align:middle">
<span title="Remove"
style="font-size:18px;color:red;cursor:pointer;"
class="icon-cross2"
ng-click="removelocationrack(r.ReceiptLocationId)"></span>
</td>
</tr>
最后,您可以在 Controller 中访问您选择的 ReceiptLocationId
,如本演示输出所示:
angular.forEach($scope.ReceiptDetail.ReceiptLocationList, function (receiptLocationListItem) {
console.log(receiptLocationListItem.ReceiptLocationId);
});
关于javascript - 使用 Angular js 在编辑操作期间设置多个 dropdownist 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43014160/
我正在使用带有 Angular js 的 ASP.NET MVC。 我有一个页面说 hello.cshtml。 其中有一个对话框,其中有按钮“添加”,单击该按钮后,我会在对话框本身具有两列的表中添加新
我是一名优秀的程序员,十分优秀!