K
K
Kirill Terentiev2016-01-18 11:40:43
JavaScript
Kirill Terentiev, 2016-01-18 11:40:43

How to add the entered numbers?

I am writing a budget calculation application, how to implement the output of the sum of entered numbers?

function TodoCtrl($scope){
  var sums = $scope.sums = [];
  $scope.sum = '200';
  $scope.addSu = function(sum){
    sums.push(sum);
  };
  $scope.removeSum = function(index){
    sum.splice(index,1);
  };
}

<!DOCTYPE html>
<html>
<head>
<script src="http://code.angularjs.org/1.1.4/angular.min.js"></script>
  <script src="todo.js"></script>
</head>
<body>
  <div ng-app>
  
  <div ng-controller="TodoCtrl">

    <h2>Сегодня я потратил: {{sum}} руб.</h2>
    <input type = 'text' ng-model = "sum" size = "40" />
    <button ng-click = "addSu(sum)">Добавить</button>
    <ul>
      <li ng-repeat = "sum in sums track by $index">
        <span ng-bind="$parent.$eval(sum)"></span>
        <span>руб</span>
      </li>
    </ul>
</div>
</div>
</body>
</html>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2016-01-18
Protko @Fesor

just read the sum in the controller, nothing like that.
put it in a separate function and call it when adding/removing elements. And yes, less logic in templates and read the angular-styleguide.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question