<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"> </script>
</head>
<body>
<div ng-app="myApp" ng-controller="personCtrl">{{count()}}</div>
<script>
var app = angular.module('myApp', []);
app.controller('peopleController', function($scope) {
$scope.items=[1,2,3,4];
var count=0
$scope.countItems = function() {
angular.forEach($scope.items,function(value,key){
count=count+1;
});
return count;
}
});
</script>
</body>
</html>
The output displayed by the above code is 88
. The value of var count
changes based on its placement within or outside the count
function. When inside the function, it shows 4
, but when declared outside, it shows 88
. Why does this occur?