I am new to AngularJS and I have a problem with a simple invoice system that I trying to create. The problem is that the input field, which shows the cost of a product (ng-model:item.cost), does not get filled with a value depending on a choice I make in a select box.
The HTML:
<div ng-controller="CartForm">
<table class="table">
<tr>
<th>Description</th>
<th>Qty</th>
<th>Cost</th>
<th>Total</th>
<th></th>
</tr>
<tr ng-repeat="item in invoice.items" ng-controller="ProductController as productCtrl">
<td><select ng-model="selectedItem" ng-options="product.name for product in productCtrl.products">
</select></td>
<td><input type="number" ng-model="item.qty" ng-required class="input-mini"></td>
<td><input ng-model="item.cost" value="{{selectedItem.price}}"></td>
<td>€{{item.qty * selectedItem.price}}</td>
<td>
[<a href ng-click="removeItem($index)">X</a>]
</td>
</tr>
<tr>
<td><a href ng-click="addItem()" class="btn btn-small">add item</a></td>
<td></td>
<td>Total:</td>
<td>€{{total()}}</td>
</tr>
</table>
</div>
The app.js:
app.controller('CartForm',function($scope) {
$scope.invoice = {
items: []
};
$scope.addItem = function() {
$scope.invoice.items.push({
qty: 1,
description: '',
cost: 0
});
},
$scope.removeItem = function(index) {
$scope.invoice.items.splice(index, 1);
},
$scope.total = function() {
var total = 0;
angular.forEach($scope.invoice.items, function(item) {
total += item.qty * item.cost;
})
return total;
}
});