-3

I would appreciate if anyone can provide any real example which can help me to crack the interview.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
ankit paul
  • 81
  • 1
  • 15
  • if someone is giving negative response, please share your views as well. i asked any real life scenario where we should use $apply and $digest. if you don't have any views on this i would request you to please ignore this question. – ankit paul Sep 04 '16 at 06:40
  • You can go through [this link](https://github.com/angular/angular.js/wiki/When-to-use-$scope.$apply()) – Pankaj Parkar Sep 04 '16 at 06:51
  • "Give me code" is not a question. That's why you are getting downvoted – OneCricketeer Sep 04 '16 at 07:10
  • I asked for the code because i know the differences but if someone asks where you can use only apply instead of digest... than the code is the only way to make them understand where we should use which approach.. by the way... thanks for downvoted. – ankit paul Sep 04 '16 at 07:49

2 Answers2

1

$digest will be called once you call $apply

here is the source code

function $apply(expr) {
try {
return $eval(expr);
} catch (e) {
$exceptionHandler(e);
} finally {
$root.$digest();
}
}

$apply will just make sure all the changes in the scope values have been absorbed whereas $apply will evaluate all the expressions the it will call the $digest function as you can see in the source code.

$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.

If you have any expression in your application that needs to be evaluated before digest cycle then you should use $apply else if you only want to apply changes then you can use $digest.

Vikash Kumar
  • 1,712
  • 1
  • 11
  • 17
1

The difference is that $apply will trigger the digest cycle from the root down, if you call $digest it will trigger from whatever scope it was called from.

Shannon Hochkins
  • 11,763
  • 15
  • 62
  • 95