Sharing Top Content from the Angular-sphere.

$apply vs $digest in directive testing – Angular.js Recipes – Medium

  • $digest();What is the difference between them, and which is better and why?Problem courtesy of: Daniel RosemanSolutionscope.
  • $digest() will fire watchers on the current scope, and on all of its children, too.
  • The first one is faster, as it needs to evaluate watchers for current scope and its children.
  • The second one is slower, as it needs to evaluate watchers for$rootScope and all it’s child scopes.When an error occurs in one of the watchers and you use scope.
  • $apply uses a try-catch block internally and passes all exceptions to $exceptionHandler.Solution courtesy of: Tadeusz WójcikView additional discussion.

If I have a directive that responds to the status of a particular attribute on the scope, and I want to change that attribute in my test and verify that it responds correctly, which is the best way…

ProblemIf I have a directive that responds to the status of a particular attribute on the scope, and I want to change that attribute in my test and verify that it responds correctly, which is the best way of doing that change?I’ve seen both these { scope.myAttribute = true;});andscope.myAttribute = true;scope.$digest();What is the difference between them, and which is better and why?Problem courtesy of: Daniel RosemanSolutionscope.$digest() will fire watchers on the current scope, and on all of its children, too. scope.$apply will evaluate passed function and run $rootScope.$digest().The first one is faster, as it needs to evaluate watchers for current scope and its children. The second one is slower, as it needs to evaluate watchers for$rootScope and all it’s child scopes.When an error occurs in one of the watchers and you use scope.$digest, it’s not handled via $exceptionHandler service, so you need to handle exception yourself. scope.$apply uses a try-catch block internally and passes all exceptions to $exceptionHandler.Solution courtesy of: Tadeusz WójcikView additional discussion.

$apply vs $digest in directive testing – Angular.js Recipes – Medium