Talking about Angular.js Use $ Watch Listening Model Change

$ watch simple to use

$ watch function is a scope for change listener model, it will notify you when your model is part of the change.

  $ watch (watchExpression, listener, objectEquality);  

The description of each parameter are as follows:

  • watchExpression: listening objects, which may be angular expression as a ‘name’, or function, such as function () {return $}.
  • listener: watchExpression changes when the called function or expression, it accepts three parameters: newValue (new value), oldValue (old value), scope (scope reference)
  • objectEquality:. whether the change depth listening, if set to true, it tells Angular checking the monitored object of each attribute if you want to attribute individual elements of monitoring array or object instead of a normal value, then you should it is used

For chestnut:

  $ = 'hello'; var watch = $ scope . $ watch ( 'name', function (newValue, oldValue, scope) {console.log (newValue); console.log (oldValue);}); $ timeout (function () {$ = "world"; }, 1000);  

$ WATCH Performance Issues Too many $ watch will result in performance issues, $ watch If we are not used, we best release it off .

The $ watch function returns a function of logout listening, if we want to monitor an attribute, then you can use it later, you can use the following way:

Var watch = $ scope. $ watch (‘Somemodel.someProperty’, Callback); // … Watch ();
There are 2 and $ Watch Related functions:

$ WatchGroup (WatchGroup (WatchCell); $ WatchCollection (Obj, Listener);
   The above is all the content of this article, I hope to help everyone, I hope everyone will support Tumi Clouds. 
© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment