While reading about Angular data-binding - came across advice -
"Due to the nature of JavaScript itself and how it passes by value vs. reference, it’s considered a best-practice in Angular to bind references in the views by an attribute on an object, rather than the raw object itself."
source: ng-book
Question: 1. What does it mean - binding references rather than objects?
This is accompanied by code snippet.
JS Code:
var module = angular.module("app", []);
//controller uses $timeout
module.controller("MyController", function ($scope, $timeout) {
var updateClock = function () {
$scope.clock = {};
$scope.clock.now = new Date();
$timeout(function () {
$scope.clock.now = new Date();
updateClock();
}, 1000);
};
updateClock();
})
HTML:
<body data-ng-app="app">
<div data-ng-controller="MyController">
<h5>Hello {{clock.now}}</h5>
</div>
</body>
Question:
2. If I remove this line $scope.clock.now = new Date();
from outside the $timeout
- it does not work. Although clock.now
is being assigned date object in $timeout
. Why?