使用Angular JS获取总td值

时间:2015-10-19 06:36:53

标签: angularjs

我试图使用Angular JS来增加总价格。

<table>
    <tr>
      <th>Price</th>
    </tr>

    <tr>
      <td>10</td>
    </tr>
<tr>
      <td>20</td>
    </tr>
<tr>
      <td>30</td>
    </tr>
<tr>
      <td>40</td>
    </tr>
<tr>
      <td>50</td>
    </tr>
    <tr>
      <td>Total Price</td>
    </tr>
</table>

2 个答案:

答案 0 :(得分:1)

这是实现使用ng-init要求的可能方式:

<table ng-init="items.total = {}">
<tr>
  <td>name</td>
  <td>numberofyears</td>
  <td>amount</td>
  <td>intrest</td>
</tr>
<tr ng-repeat="item in items">
  <td>{{item.name}}</td>
  <td ng-init="items.total.numberofyears = items.total.numberofyears + item.numberofyears">{{item.numberofyears}}</td>
  <td ng-init="items.total.amount = items.total.amount + item.amount">{{item.amount}}</td>
  <td ng-init="items.total.interest = items.total.interest + item.interest">{{item.interest}}%</td>
</tr>
<tr>
  <td>Total</td>
  <td>{{items.total.numberofyears}}</td>
  <td>{{items.total.amount}}</td>
  <td>{{items.total.interest}}%</td>
</tr>
</table>

答案 1 :(得分:0)

做这样的事情:

HTML:

<table>
    <tr ng-repeat = "price in prices">
        <td ng-bind="price.price"><td>
    </tr>
    <tr>
        <td ng-bind="calculateTotal()"><td>
    </tr>
</table>

JS:

$scope.prices = [{
  "price": "5"
}, {
  "price": "10"
}, {
  "price": "13"
}];


$scope.calculateTotal = function() {
  var total = 0;

  for (i = 0; i < $scope.prices.length; i++) {
    total += +$scope.prices[i].price; // (+) sign before variable is used to convert string values to number
  }

  return total;
};