我正在尝试通过ng-repeat更新显示的数组的特定数据。
给出数据:
$scope.cartItems = [];
$scope.items = [{code: 'ABC', cash:'20.00', charge: '25.00'},
{code: 'DEF', cash:'10.00', charge: '15.00'},{code:
'GHI',cash:'30.00', charge: '35.00'}];
那我有这张桌子
Item | Price | Action
ABC | 20.00 | Add
DEF | 10.00 | Add
ng-click="add(Item,Price)"
$scope.add = function(Item,Price){
var itemscart = {};
itemscart.code = Item;
itemscart.price = Price;
$scope.cartItems.push(itemscart)
}
,并通过html表显示。
ng-repeat="cart in cartItems"
Item | price | bracket
ABC | 20.00 | Cash //dropdown
DEF | 10.00 | Cash //dropdown
我有带有选项值的下拉选择标签; “现金”或“收费”
如果在下拉列表中我选择了收费。 html表的预期输出必须为:
Item | price | braket
ABC | 25.00 | Charge //dropdown
DEF | 15.00 | Charge //dropdown
我尝试过:
if($scope.optionval == 'Charge'){
var price = 0;
$scope.cartItems.forEach(function(v){
if(v.code == ($scope.items.forEach(function(c){c.code; price =
c.charge; })))
v.PRICE = price;
console.log(v.PRICE) //gets the last row
console.log(price) //null
});
}
答案 0 :(得分:0)
根据显示和预期的表格,您可以尝试以下操作:
答案 1 :(得分:0)
我认为您可以使用其他方法,例如使用shrink或Index alias指令来获取您要达到的结果
尝试在$ scope中添加一个自定义字段以存储应显示的价格,然后有两种选择:修改每个字段或单独修改:
独立显示:
app.js
$scope.items = [{code: 'ABC', cash:'20.00', charge: '25.00', displaying:'cash'},{...}
HTML
<tr>
<th>Item</th>
<th>Price</th>
<th></th>
</tr>
<tr ng-repeat="cart in whatever">
<td>{{cart.item}}</td>
<td ng-show="cart.displaying=='cash'>{{cart.cash}}</td>
<td ng-show="cart.displaying=='charge'>{{cart.charge}}</td>
<td>{{cart.displaying}}</td>
<td>*Dropdown to modify this specific 'displaying' field*</td>
</tr>
同时输入每个字段:
app.js
$scope.displayingitems = 'cash'
HTML
*Dropdown to modify this specific 'displaying' field*
<tr>
<th>Item</th>
<th>Price</th>
<th></th>
</tr>
<tr ng-repeat="cart in whatever">
<td>{{cart.item}}</td>
<td ng-show="displayingitems=='cash'>{{cart.cash}}</td>
<td ng-show="displayingitems =='charge'>{{cart.charge}}</td>
<td>{{displayingitems}}</td>
<td></td>
</tr>