Cery De
Cery De

Reputation: 49

how to get all ng-model's value in ng-repeat

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

Answers (1)

Anik Islam Abhi
Anik Islam Abhi

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()}}

JSFIDDLE

Upvotes: 1

Related Questions