Sum of 2 values with button in Angularjs

Asked

Viewed 249 times

0

I wonder what’s going wrong because when I do the ng-click in HTML the {{resultado}} appears as Nan.

HTML

        <button class="button button-full button-positive" ngclick="juntarTotal()">
          Juntar os dois campos
        </button>

        <h3>{{resultado}}</h3>

controller js.

angular.module('app.controllers', [])

.controller('doisnumerosController', function($scope){
    $scope.juntarTotal = () => {
      $scope.resultado = $scope.valor1 + $scope.valor2;
    }
});
  • First show the values of $scope.valor1 and value2, see if it has value and if they are numerical. If necessary convert to numbers to sum, example Number($scope.valor1)

  • My input is "number". $scope.juntarTotal = () => { &#xA;$scope.resultado = Number($scope.valor1) + Number($scope.valor2;) } @Ricardopunctual, type like this?

  • This, to ensure that values are summed up as numbers

  • But it’s not giving, the value of {{result}} in html continues giving Nan, can help me with some example @Ricardopunctual?

  • Nan means it is not a number (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN)

  • It means one of your values is not number, you need to see what it is.. in the example you put, the semicolon is inside the parentheses of Function which gives error: Number($scope.valor2;) if you used so in the code gives error, the correct is Number($scope.valor2);

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.