Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 985 of the free egghead.io lessons, plus get Angular 1.x content delivered directly to your inbox!

Existing egghead members will not see this. Sign in.

$apply vs $digest

2:25 Angular 1.x lesson by

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

Get the Code Now
click to level up

egghead.io comment guidelines


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

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.

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.

I can keep on clicking that. Digest just controls that inner controller.

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.

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.

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

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.

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.

Otherwise, you should be fine using apply and avoiding digest altogether.

Joel's Head
Why are we asking?