Angularjs: addition of integers even after I parse the variable as integer
Posted
by
Shiv Kumar
on Stack Overflow
See other posts from Stack Overflow
or by Shiv Kumar
Published on 2013-10-30T08:30:42Z
Indexed on
2013/10/30
9:54 UTC
Read the original article
Hit count: 333
JavaScript
|angularjs
I really have a weird problem in adding two numbers.
Here is my code, in the first controller everything is working fine, but in the second controller instead of 0 if I add 10, the output is completely weird
Here is html code
<div ng-app="">
<div ng-controller="Controller1">
<br/>**** Controller-1
<br/>Add 0 : {{update1(0)}}
<br/>Add 10 : {{update1(10)}}
<br/>Add 50 : {{update1(50)}}
<br/>Add -60 : {{update1(-60)}}</div>
<div ng-controller="Controller2">
<br/>**** Controller-2
<br/>Add 10 : {{update2(10)}}
<br/>Add 10 : {{update2(10)}}
<br/>Add 50 : {{update2(50)}}
<br/>Add -60 : {{update2(-60)}}</div>
</div>
Here is my javascript
function Controller1($scope) {
var x = 0;
$scope.update1 = function (smValue) {
x += parseInt(smValue);
return x;
}
}
function Controller2($scope) {
var y = 0;
$scope.update2 = function (smValue) {
y += parseInt(smValue);
return y;
}
}
and here is the output
**** Controller-1
Add 0 : 0
Add 10 : 10
Add 50 : 60
Add -60 : 0
**** Controller-2
Add 0 : 110
Add 10 : 120
Add 50 : 170
Add -60 : 110
here is the link to try: http://jsfiddle.net/6VqqN/
can anyone please explain me why it is behaving like that. Even if I add a 3or4 digit number, output is completely different then what I expected.
© Stack Overflow or respective owner