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.
console.log()statements: jsfiddle.net/6VqqN/2. Looks likeupdate1()gets called four times (like you'd expect), butupdate2()gets called repeatedly until Angular crashes.{{update()}}call passes a non-zero value, but the problem does not occur with either controller if the first value is zero. (I don't know why, but maybe that helps somebody else explain it.)Controller1value ofxis returned to 0. And that is why the effect is not visible. The question is why are they getting called too many times?