$apply vs $digest

John Lindquist
InstructorJohn Lindquist
Share this video with your friends

Social Share Links

Send Tweet
Published 10 years ago
Updated 5 years ago

$apply and $digest are both methods on AngularJS scopes that allow you to manually trigger the updates to bound properties on your scopes.

Man: [00:00] This example has an outer controller, a middle controller, and an inner controller, all using the time variable, which is on the scope. This is intended to show the difference between digest, so this button will say "Digest," and this button will say, "Apply," from inside this inner controller scope.

[00:21] Once I click on digest you can see that it's only changing the scope that's inside this inner controller. I'll click it again. You can see it's only changing that. Once I click on apply, you'll see that it's changing everything outside of my controller, as well. Outer controller, middle controller, and inner controller.

[00:40] I can keep on clicking that. Digest just controls that inner controller.

[00:45] In the actual code, I'm breaking a lot of best practices in my controllers. I'm accessing the element to do that, border highlighting and stuff like that. I have a stupid filter on here just to show that filters get called when scope apply is called, as well. That's definitely something to watch out for. If you have a really heavy filter that's going to get invoked any time that you run scope apply that might be an indication you should use scope digest instead.

[01:13] On my digest buttons I'm using root scope just to show that it's a scope variable that's shared between everything. Even though this time is on the root scope, when I click digest even though that's on the root scope this is still only updating here. You can see digest, I'm updating time on the root scope but I'm digesting on my scope. That isn't being applied to the other scopes.

[01:38] When you think of digest, you can think only digest on this scope. When you think of an apply, you can say, "We'll digest on the root scope and then cascade down into every child scope that the root scope has created."

[01:54] That's the major difference between digest and apply. You have digesting on only your scope and apply is digesting on the root scope, which will go through every single scope.

[02:03] You don't really want to use digest unless you need to. Don't go pre-optimizing things that don't need to be optimized. Only use digest when you're aware of super heavy filters that would be called every time you use apply or other really heavy lifting that might happen when you use apply.

[02:21] Otherwise, you should be fine using apply and avoiding digest altogether.

egghead
egghead
~ 5 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today