0

Below is my controller. I want to convert 'form.paymentmade+ newpayment' to numbers; presently the values are just concatenating as strings.

Say if the value of form.paymentmade = 3 and then I entered newpayment = 45, the result=34555555555. I guessed it's because both have not been parsed as int.

<script> 
      function TodoCtrl ($scope) { 

      $scope.$watch ('form.paymentmade   + newpayment' , function (value) {
      $scope.form.paymentmade = value;

  });

 }
</script>

Here's what I tried, but it's not working:

<script> 
function TodoCtrl ($scope) { 

  $scope.$watch (parseInt(form.paymentmade )  + parseInt (newpayment), function (value) {
$scope.form.paymentmade = value;

  });

 }

</script>

How do I parse to int and sum in angularjs?

hunch_hunch
  • 2,283
  • 1
  • 21
  • 26
brother
  • 1
  • 2
  • Can you share a link where this is being used? Or can you create a codepen and share the context of what you're trying to do? – Aniket Suryavanshi May 17 '18 at 20:08
  • 1
    $watch is used to watch for variable changes. It's parameter 'form.paymentmade + newpayment' looks incorrect. It should be a variable name. – Aniket Suryavanshi May 17 '18 at 20:15
  • Thanks for your reply. form.paymentmade + newpayment' are from the HTML ng-model of the two input form fields. I wanted to $watch changes on 'newpayment' and use the changed value added to form.paymentmade. How do I create a codepen? – brother May 17 '18 at 20:25
  • You can create a snippet here https://codepen.io/pen/ If you have multiple concerned AngularJS files, you can create snippet at https://plnkr.co/edit/?p=catalogue – Aniket Suryavanshi May 17 '18 at 20:35
  • Use a function, like `$scope.$watch(function () { return parseInt(form.paymentmade, 10) + parseInt(newpayment); }, ...});` See e.g., [this answer](https://stackoverflow.com/q/17397996/215552) – Heretic Monkey May 18 '18 at 00:38
  • 1
    Possible duplicate of [New to Angular - Computed Variables](https://stackoverflow.com/questions/17173768/new-to-angular-computed-variables) – Heretic Monkey May 18 '18 at 00:41
  • Use of `$watch` in this manner is a [code smell](https://en.wikipedia.org/wiki/Code_smell), a symptom of a deeper problem. Computations of this kind are better served by `ng-change` or other reactive computation. Without understand the complete context it is hard to write an adequate answer. – georgeawg May 18 '18 at 23:09

2 Answers2

0
   <script>
              $scope.$watch( “newPayment”, function (newValue){
                     $scope.form.paymentMade = parseInt($scope.form.paymentMade) + parseInt(newValue);
              })
    </script>
Habeeb
  • 322
  • 1
  • 8
  • Well, Mike McCaughan and Habeebur Rahman your answers really help, the challenge now is that the $watch keeps adding the existing values without clearing what it $watch before. It doesn't $watch on keypress or pick just the new number and calculate it adds the whole field again. e.g if $scope.form.paymentmade = 100. And I enter newpayment = 1. $scope.form.paymentmade =101 then I increase newpayment value with 1 , now its = 11. But $scope.form.paymentmade = 101 + 11 instead of 100 + 11. It sums the whole field again. Is there a way to just sum the new numbers I add not per each chang – brother May 18 '18 at 17:14
0

Try This

<script>    
       $scope.$watch( “newPayment”, function (newValue, oldValue){
            if(oldValue != undefined){
                $scope.form.paymentMade = parseInt($scope.form.paymentMade) + parseInt(newValue) -  parseInt(oldValue);
            }else{
                $scope.form.paymentMade = parseInt($scope.form.paymentMade) + parseInt(newValue);
            }

        })
 </script>
Habeeb
  • 322
  • 1
  • 8