This Lesson is for Members

Subscribe today and get access to all lessons! Plus direct HD download for offline use, enhances transcripts, member comment forums, and iTunes "podcast" RSS feed. Level up your skills now!

Unlock This Lesson

Already subscribed? Sign In

Autoplay

    $apply vs $digest

    John LindquistJohn Lindquist

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

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    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.

    Discuss

    Discuss