0

Hi I have a Json Like this

[
  {
    "id": 1,
    "name": "Furniture & Fixture",
    "choices": [
      {
        "req_goods": "",
        "qty": "10",
        "rate": "",
        "total": ""
      }
    ]
  },
  {
    "id": 2,
    "name": "Miscellaneous Property",
    "choices": [
      {
        "req_goods": "",
        "qty": "",
        "rate": "",
        "total": ""
      }
    ]
  },
  {
    "id": 3,
    "name": "Office Equipment",
    "choices": [
      {
        "req_goods": "",
        "qty": "",
        "rate": "",
        "total": ""
      }
    ]
  }
]

here choices are my dynamic fields user can add as much choices as they want, my add/remove js is like this

$scope.addNewChoice = function(id){
        $scope.capital_budgets[id].choices.push({});
    };
    $scope.removeChoice = function(parent_id,id) {      
        $scope.capital_budgets[parent_id].choices.splice(id,1);
      };

my html

<div ng-repeat="cb in capital_budgets">
  <div><b><% $index+1 %> <% cb.name %></b></div>                  

  <div ng-repeat="choice in cb.choices">    
    <input type="text" ng-model="choice.req_goods">                 
    <input type="text" ng-model="choice.qty">
    <input type="text" ng-model="choice.rate">
    <input type="text" ng-model="choice.total">

   <button type="button" ng-hide="$first" ng-click="removeChoice($parent.$index,$index)">-</button>

  </div>
  <button type="button" ng-click="addNewChoice($index)">+</button>

</div>

Now I want to calculate the total(qty*rate) of each added choices whenever they put qty and rate and put it in total field please help

4

1 回答 1

2

实现此目的的一种方法是在您的控制器中创建一个方法来进行计算,然后在更改qtyor时调用它rate

控制器:

$scope.updateTotal = function(choice) {
    if(!choice.qty || !choice.rate) {
        choice.total = 0;
    } else {
        choice.total = choice.qty * choice.rate;
    }
};

html:

<input type="text" ng-model="choice.qty" ng-change="updateTotal(choice)">
<input type="text" ng-model="choice.rate" ng-change="updateTotal(choice)">
于 2016-09-17T15:13:04.320 回答