Reputation: 49
i want to get all ng-model's value in ng-repeat here is my html
<div ng-repeat="kind in plans.availableOptions">
<span class="payLabel text-left">{{kind.name}}</span>
<input class="payLabel" type="text" ng-model="kind.number" />
{{priceAll}}<!--priceAll is the sum of the input-->
</div>
then here is my controller
$scope.plans={availableOptions:[{name:'by cash',id:'1'},{name:'by credit',id:'2'}],otherParam:{}}
$scope.priceAll=0;
i'm a green hand of angular. how can i get the sum of the number of the input
Upvotes: 2
Views: 119
Reputation: 25352
Try like this
$scope.sum = function() {
return $scope.plans.availableOptions.filter(function(x) {
return x.number;
}).map(function(x) {
return x.number;
}).reduce(function(a, b) {
return +a + +b;
}, 0);
}
HTML
<div ng-repeat="kind in plans.availableOptions">
<span class="payLabel text-left">{{kind.name}}</span>
<input class="payLabel" type="text" ng-model="kind.number" />
<!--priceAll is the sum of the input-->
</div>
{{sum()}}
Upvotes: 1