0

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?

4

1 回答 1

2

好吧,scope.$watch应该这样做,马上开火。这是最初的更新。它会进行初始触发,然后任何后续触发仅因实际更改而发生。您可以检测初始运行 if newValue === oldValue,其中这些是您的手表回调的第一个和第二个参数。

至于为什么不再调用它,您是否在正常 Angular 上下文之外更改了范围属性?如果是这样,您将需要一个scope.$apply.

于 2012-12-18T04:41:29.307 回答