Consider the following html file with an angular script:
<!DOCTYPE html>
<html>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<head>
</head>
<body ng-app="TestFirstApp" ng-controller="TestCtrl">
<script>
var app = angular.module('TestFirstApp', []);
app.controller('TestCtrl', function($scope) {
$scope.amt = 50000;
$scope.terms = 1;
$scope.rate= 10;
$scope.interestamt= function() {
console.log("New value of amt, terms and rate: "+$scope.amt+ " "+$scope.terms+" "+$scope.rate);
return ($scope.amt*$scope.rate*$scope.terms)/100;
};
});
</script>
<div>
<label>Enter Amount</label><input type="number" ng-model="amt"></input>
<br>
<label>Enter Years</label><input type="number" ng-model="terms"></input>
<br>
<label>Enter Rate P.A.</label><input type="number" ng-model="rate"></input>
<br>
<label>The interest amount is : {{ interestamt() }}</label>
</div>
</body>
</html>
When I open the page in browser and change any value in the input boxes, as expected due to binding the interest amount is calculated and the value updates in the label tag.But in the console, I noticed that the log statement gets printed 3 times every time i change either of the three values in the input boxes, meaning the function gets called multiple times. Can anyone explain why is the function getting called multiple times, instead of only once when any value is changed?