Reputation: 2423
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?
Upvotes: 3
Views: 798
Reputation: 3020
As for question 2: basically if you remove the $scope.clock.now = new Date(); instruction from outside the $timeout callback, in between two digest cycle triggered by the $timeout service, $scope.clock is actually
So the $scope.clock.now variable is always undefined at digest time.
Detailed execution steps from Updateclock function execution start until digest cycle triggered by $timeout service :
Upvotes: 1
Reputation: 24119
Q1:
this
and scope context{{this.now}}
vs {{clock.now}}
Q2:
Here are three examples, I prefer the syntax of Controller1
it makes debugging easier imo:
var module = angular.module("app", []);
module.controller("Controller1", function ($timeout) {
var vm = this
vm.clock = {now: 'its loading'};
vm.now = new Date();
var updateClock = function () {
$timeout(function () {
vm.clock.now = new Date();
updateClock();
}, 3000);
};
updateClock();
})
module.controller("Controller2", function ($scope, $timeout) {
$scope.clock = {now: 'its loading'};
$scope.now = new Date();
var updateClock = function () {
$timeout(function () {
$scope.clock.now = new Date();
updateClock();
}, 3000);
};
updateClock();
})
module.controller("Controller3", function ($timeout) {
var vm = this
var updateClock = function () {
$timeout(function () {
try {
vm.clock.now = new Date();
} catch(e){
vm.clock = {}
vm.clock.now = 'oops'
}
updateClock();
}, 3000);
};
updateClock();
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body data-ng-app="app">
<div data-ng-controller="Controller1 as vm">
<h6>vm.clock.now {{vm.clock.now}} vm.now {{vm.now}}</h6>
<h6>vm: {{vm}}</h6>
</div>
<div data-ng-controller="Controller2">
<h6>$scope.clock.now {{clock.now}} $scope.now {{this.now}}</h6>
<h6>this is confusing {{this}}</h6>
</div>
<div data-ng-controller="Controller3 as vm">
<h1>{{vm.clock.now}}</h1>
<h6>nice scope: {{vm}}</h6>
</div>
</body>
Also see:
Upvotes: 1