如何在ng-repeat中获得所有ng-model的值

时间:2015-12-31 02:28:14

标签: javascript angularjs

我想在ng-repeat中得到所有ng-model的值,这是我的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>

然后这是我的控制器

$scope.plans={availableOptions:[{name:'by cash',id:'1'},{name:'by credit',id:'2'}],otherParam:{}}
$scope.priceAll=0;
我是一个有角度的绿手。我怎样才能得到输入数的总和

1 个答案:

答案 0 :(得分:1)

试试这个

  $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