I am working with a code snippet that utilizes AngularJS:
var app = angular.module('Demo', []);
app.controller('DemoCtrl', function ($scope) {
function notify(newValue, oldValue) {
console.log('%s => %s', oldValue, newValue);
}
$scope.$watch('collection.length', notify);
$scope.$watch('my', notify);
$scope.collection = [];
$scope.my = 'hello';
});
The $watch
function fires initially. When running this code snippet, the following output is generated:
0 => 0
hello => hello
Is this behavior correct? While I could check values for equality, what are the reasons behind this particular behavior?
P.S. You can try out this sample online: http://jsbin.com/otakaw/7/edit