Why is the scope function called several times?

<!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">

 First Name: <input type="text" ng-model="firstName"><br>
 Last Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{fullName()}}

</div>

<script>
var app = angular.module('myApp', []);
app.controller('personCtrl', function($scope) {
$scope.firstName = "John";
$scope.lastName = "Doe";
$scope.fullName = function() {
 console.log("inside");
    return $scope.firstName + " " + $scope.lastName;
}
});
</script>

</body>
</html>

the console log value is displayed inside three times. I'm new to angularjs, so please be so kind as to let me know why this is happening.

+4
source share
2 answers

The scope function is called every time angular throws a scope change event. It is built in to fake the ES6 feature called Object.watch.

So it doesn’t matter that it is called multiple times, this is just an angular way to check for changes;)

+5
source

$scope.fullName - , firstName lastName, .log, .

+1

Source: https://habr.com/ru/post/1583957/


All Articles