Add Values in Angularjs

Asked

Viewed 3,658 times

0

I’m 5 inputs where I enter some values, but when I try to sum the total result is not displayed in the last input. HTML:

 <label class="item item-input">
    <span class="cinza input-label">Valor 1</span>
    <input type="number" ng-model="var1"> </input></label>
    <label class="item item-input">
    <span class="cinza input-label">Valor 2</span>
    <input type="number" ng-model="var2"> </input></label>
    <label class="item item-input">
    <span class="cinza input-label">Valor 3</span>
    <input type="number" ng-model="var3"> </input></label>
    <label class="item item-input">
    <span class="cinza input-label">Valor 4</span>
    <input type="number" ng-model="var4"> </input></label>
    <label class="item item-input">
    <span class="cinza input-label">Valor 5</span>
    <input type="number" ng-model="var5"> </input></label>
    <div ng-controller="Total">
    <label class="item item-input">
    <span class="cinza input-label">Total</span>
    <input type="number" ng-model="total"> </input></label></input> 
</div>

Controller:

.controller('Total', function ($state, $scope) {

$scope.var1 = 0;
$scope.var2 = 0;
$scope.var3 = 0;
$scope.var4 = 0;
$scope.var5 = 0;

var total = $scope.var1 + $scope.var2 + $scope.var3 + $scope.var4 + $scope.var5;

$scope.total = total;

})

How do I display total value in an input?

3 answers

4

The statement of the other answer is true and works, but as an alternative, if you want to put the value in the total property, a self-executable function can be used, such as:

$scope.total = (function(){
   $scope.var1 + $scope.var2 + $scope.var3 + $scope.var4 + $scope.var5;
})();
  • I need to display this Total at the last input.

3


It is not working because you do not call any action (function) that does the sum.

Try the following:

At its last input put a ng-Blur or ng-click

<input type="number" ng-click="somarValores()" ng-blur="somarValores()" ng-model="var5"> </input></label>

And in your Controller:

$scope.somarValores = function(){
    $scope.total = $scope.var1 + $scope.var2 + $scope.var3 + $scope.var4 + $scope.var5;
}

Working example: http://codepen.io/anon/pen/KdJmwq

OBS: An excerpt of your html can present problems too, I don’t know how the whole structure is but I noticed that you call the Controller only at the end of html. Thus all variables declared in Controller are only "seen" at the end.

  • Dude, I’m already able to add it up, but he’s only filling in the total field after I put the value in all the fields... Take a look here: http://codepen.io/anon/pen/JYxJEG I want it just like this, I just want it to add each time I fill in a field, without having to fill in all.

  • In your Controller you have to initialize the variable with 0 and not " ": http://codepen.io/anon/pen/KdJmwq

  • I cannot display the numbers 0 in the input.

  • Then you’ll have to change your logic a little bit. Keep trying, I’ll be back for lunch to help you

  • I tried some things but none worked out... that link was the closest I got haha

  • Face I already n know if it works, because from what I analyzed you need to initialize the variable as 0. You want to update in "real time" IE, when entering with a number it starts the sum, but if you do not initialize the variable will not work

  • Here’s something closer to what you need: http://codepen.io/anon/pen/KdJmwq

  • But it only works if you set all the numbers

  • Baah man, thanks for the help, I’ll keep trying here... If you have any ideas you can let me know, if I can also let you know.

  • All right, good luck :D

  • I got it, man: http://codepen.io/anon/pen/JYxJEG

Show 6 more comments

0

Browser other questions tagged

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