Why does the watch fire when oldvalue is equal to newvalue?

I have:

   $scope.$watch('option.sSubject', function (newValue, oldValue) {
                    if (newValue !== oldValue) {

and:

console.log(newValue)
1
undefined
console.log(oldValue)
1
undefined

Can someone explain why the watch fires when there seems to be no change in value. Note that this is when I am just opening the page.

Answers


Quote from the API documentation of $scope.$apply():

After a watcher is registered with the scope, the listener fn is called asynchronously (via $evalAsync) to initialize the watcher. In rare cases, this is undesirable because the listener is called when the result of watchExpression didn't change. To detect this scenario within the listener fn, you can compare the newVal and oldVal. If these two values are identical (===) then the listener was called due to initialization.


Need Your Help

How can I round money values to the nearest $5.00 interval?

c++ sql informix

I have an Informix-SQL based Pawnshop app which calculates an estimate of how much money should be loaned to a customer, based on the weight and purity of gold. The minimum the pawnshop lends is $5...