AngularJS: Cart example

by Sajeetharan Sinnathurai

HTML

<link rel="stylesheet" href="http://d1e24pw9mnwsl8.cloudfront.net/c/bootstrap/css/bootstrap.min.css">
<h2>Shopping Card Example</h2>
<div ng:controller="CartForm">
    <table class="table">
        <tr>            
              <th>1</th>
            <th>2</th>
            <th>3</th>
            <th>4</th>
            <th></th>
        </tr>
        <tr ng:repeat="item in invoice.items">
            <td><input type="text" ng:model="item.one"class="input-small"></td>           
            <td><input type="number" ng:model="item.two" ng:required class="input-mini"></td>
            <td><input type="number" ng:model="item.three" ng:required class="input-mini"></td>
          
            <td>{{total(item)}}</td>
        </tr>
        <tr>
            
           
        </tr>
    </table>
</div>

JavaScript

function CartForm($scope) {
    $scope.invoice = {
        items: [{
            one: 100,
            two: 100,
            three: 100}
          ]
    };

    $scope.total = function(item) {
         
        var total = 0;
        angular.forEach($scope.invoice.items, function(item) {
            total = parseInt(item.one) + parseInt(item.two) +parseInt( item.three);
        })

        return total;
    }
}