Reputation: 718
I'm writing a controller that reacts to an update on a given controller property change. I'm writing tests for it, but I realised that the $watch callback is being called before the code that updates the model.
Moreover, after the premature $watch callback invocation and the code that updates the model, the $watch callback is not called again.
It seems to me that the $watch callback is being called during the controller instantiation and the expression observer is not configured, or the like.
What do you guys think?
Upvotes: 0
Views: 105
Reputation: 28110
Well, scope.$watch
is supposed to do that, fire right away. That's the initial update. It does the initial firing then any subsequent firings only happen because of real changes. You can detect the initial run if newValue === oldValue
, where those are the first and second parameters to your watch callback.
As to why it's not called again, are you changing your scope properties outside of the normal Angular context? If so, you'll need a scope.$apply
.
Upvotes: 2