1

{ "specific": { "first":{ "title":"222", "items":[ { "z": "1", "s":"2", "p":"3", }, { "z": "12", "s":"12", "p":"12"
} ], "results":{ "z": "13", "s":"14", "p":"15" } } } }

<div class="row" ng-repeat="(item, q)  in first.items">
  <input ng-model="q.z">
  <input ng-model="q.s">
  <input ng-model="q.p">

Results should calculate the values for repeated elements in items (for example, item1.z + item2.z = results.z).

How can I calculate the values for each repeated element for item in items with angularJS?

1

1 Answer 1

1

Trying to like this.

In Template

<td>Total: {{ getTotal() }}</td>

In Controller

$scope.getTotal = function(){
    var total = 0;
    for(var i = 0; i < $scope.cart.products.length; i++){
        var product = $scope.cart.products[i];
        total += (product.price * product.quantity);
    }
    return total;
}
Sign up to request clarification or add additional context in comments.

3 Comments

$scope.getTotal = function(){ var total = 0; for(var i = 0; i < $scope.first.items.length; i++){ var item = $scope.first.items[i]; total += item.z; } return total; } as a result it returns 112 (item1 z, item2 z), the same result if use <div ng-init="items.total.z = items.total.z + q.z"></div> What i'm missing?
try to this. <div ng-init="items.total.z = (items.total.z) + (q.z)"></div>
fixed with ` total = total + parseInt(item.z);` Thanks for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.