我有一個按鈕來向表中添加新行,我的問題是ng模型的值。它的目標是從下拉列表中選擇一個選項,在其他字段中顯示來自該項目的所有數據,以及創建新行時,所有ng模型具有相同的值。如果您選擇2次相同的設備並且添加一個值,它會在兩者中更新,並且只需要在其中一箇中更新。表格內容在每一行中都是獨立的
下面的代碼與工作的例子當前狀態的例子http://jsfiddle.net/z06sonnt/5/
<div ng-app>
<div ng-controller="Ctrl" id="midi-ctrl">
<button ng-click="addRow()">Add Row</button>
<table>
<tr>
<th></th>
<th>Type</th>
<th>Product</th>
<th>Price</th>
<th>Quantity</th>
<th>Notes</th>
<th>Cost</th>
</tr>
<tr ng-repeat="row in rows">
<td><strong>{{row.id}}</strong>
</td>
<td align="center">{{row.selectedItem.type}}</td>
<td align="center">
<select ng-model="row.selectedItem" ng-options="item.product for item in products">
<option value="">Select Product</option>
</select>
</td>
<td>{{row.selectedItem.price}} </td>
<td align="center">
<input style="width: 75px" ng-model="row.selectedItem.quantity" ng-change="calcPrice(row.selectedItem)" type='number'>
</td>
<td align="center">
<input style="width: 75px" ng-model="row.selectedItem.notes">
</td>
<td align="center">
<input style="width: 75px" ng-model="row.selectedItem.cost">$
</td>
</tr>
</table>
<b>rows</b>{{rows}}
<br>
<b>Products</b>{{products}}
Controller.js
function Ctrl($scope) {
$scope.rows = [{
id: '1',
selectedItem: []
}, {
id: '2',
selectedItem: []
}];
$scope.counter = 3;
$scope.calcPrice = function(item) {
item.cost = item.price * item.quantity;
};
$scope.addRow = function() {
$scope.newRow = {};
$scope.newRow['id'] = $scope.counter;
$scope.newRow['selectedItem'] = [];
$scope.rows.push($scope.newRow);
$scope.counter++;
};
$scope.products = [{
type: 'mobile',
product: 'Device A',
price: 70
}, {
type: 'games',
product: 'Device B',
price: 80
}, {
type: 'PC',
product: 'Device C',
price: 100
}, ];
}
什麼叫動態 – gaurav5430
意思,如果你提供了這個代碼的plunkr或codepen例如這將是真正的幫助。 –
@ gaurav5430了'selectedItem'領域屬於每一行就像他們加入這裏 – changez