scope.$digest vs rootScope.$digest

1,105 views
Skip to first unread message

Doron Cyngiser

unread,
Jan 23, 2013, 5:10:51 AM1/23/13
to ang...@googlegroups.com
I am trying to understand exactly how the digest is working and what is the best practice when you need to update angular with model change.
i am using websockets in order to communicate with the server, so i have to make sure a digest is being called each time i get a message from the server.

1. I use $apply most of the times, or $timeout with 0 as wait ms. , but i see examples that call the scope.$digest specifically (for example in the ng-grid project code). so what is the best practice and what is the differences?

2. If i am calling the scope.$digest manually does it fire the dirty checking for all watchers or only the scope children watchers? in angular code i see that digest is changing the $$phase of the rootscope, but it wasnt clear to me which watchers are being checked.

3. Is there any special effect that happens when there is an apply call/digest inside a directive with isolated scope?

Thanks 

Peter Bacon Darwin

unread,
Jan 23, 2013, 5:34:25 AM1/23/13
to ang...@googlegroups.com
scope.$digest only runs watchers in that scope and its children (including isolated children).  Therefore there is the danger that watchers further up the scope tree that would effect this scope are not being triggered.

scope.$apply always calls $rootScope.$digest

If you are writing a directive that is pretty isolated [sic] from other scopes then there may be times when you know that your digest will not need to trigger watches outside and could optimize to use only the local scope digest.  But these would need to be identified carefully as it is quite hard to know that there is not something outside that affects the local scope.
Pete


--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en-US.
 
 

Doron Cyngiser

unread,
Jan 23, 2013, 8:11:03 AM1/23/13
to ang...@googlegroups.com
Thanks
Reply all
Reply to author
Forward
0 new messages