Monday, 12 December 2016

$apply, $watch, $digest methods in AngularJS

There are many methods in AngularJs some of them are give below.
$watch - Registers a listener callback to be executed whenever the watchExpression changes.
The watchExpression is called on every call to $digest() and should return the value that will be watched. (watchExpressionshould not change its value when executed multiple times with the same input because it may be executed multiple times by $digest().
The listener is called only when the value from the current watchExpression and the previous call to watchExpression are not equal. example - 
scope.$watch('name', function(newValue, oldValue) {
  scope.counter = scope.counter + 1;
$watchgroup - A variant of $watch() where it watches an array of watchExpressions. If any one expression in the collection changes the listener is executed.
$apply - $apply() is used to execute an expression in angular from outside of the angular framework. (For example from browser DOM events, setTimeout, XHR or third party libraries). Because we are calling into the angular framework we need to perform proper scope life cycle of exception handlingexecuting watches.
function $apply(expr) {
  try {
    return $eval(expr);
  } catch (e) {
  } finally {
Scope's $apply() method transitions through the following stages:

  1. The expression is executed using the $eval() method.
  2. Any exceptions from the execution of the expression are forwarded to the $exceptionHandler service.
  3. The watch listeners are fired immediately after the expression was executed using the $digest() method.
$digest - Processes all of the watchers of the current scope and its children. Because a watcher's listener can change the model, the $digest()keeps calling the watchers until no more listeners are firing. This means that it is possible to get into an infinite loop. This function will throw 'Maximum iteration limit exceeded.' if the number of iterations exceeds 10.
Usually, you don't call $digest() directly in controllers or in directives. Instead, you should call $apply() (typically from within a directive), which will force a $digest().
var scope = ...; = 'misko';
scope.counter = 0;

scope.$watch('name', function(newValue, oldValue) {
  scope.counter = scope.counter + 1;

// the listener is always called during the first $digest loop after it was registered

// but now it will not be called unless the value changes
expect(scope.counter).toEqual(1); = 'adam';
$destroy - Removes the current scope (and all of its children) from the parent scope. Removal implies that calls to $digest() will no longer propagate to the current scope and its children. Removal also implies that the current scope is eligible for garbage collection.

The $destroy() is usually used by directives such as ngRepeat for managing the unrolling of the loop.
$eval - Executes the expression on the current scope and returns the result. Any exceptions in the expression are propagated (uncaught). This is useful when evaluating Angular expressions.
var scope = ng.$rootScope.Scope();
scope.a = 1;
scope.b = 2;

expect(scope.$eval(function(scope){ return scope.a + scope.b; })).toEqual(3);
$evalAsync- Executes the expression on the current scope at a later point in time.
The $evalAsync makes no guarantees as to when the expression will be executed, only that:
  • it will execute after the function that scheduled the evaluation (preferably before DOM rendering).
  • at least one $digest cycle will be performed after expression execution.

For other methods click  $digest $eval $destry, $apply etc

1 comment:

  1. Just wish to say your article is as astounding. The clearness in your
    submit is just excellent and i can suppose you’re knowledgeable in this subject.
    Well along with your permission allow me to take hold of your RSS feed to stay up to date with drawing close post. Thanks a million and please carry on the rewarding work.
    Read More:
    AngularJS Certification Training in Chennai