I'm having a little trouble understanding how to use $apply cleanly.
Suppose I have code that broadcasts and handles an event "updateFoo".
Suppose that "updateFoo" can come from two places: one external component
[[A]] that calls $rootScope.$broadcast('updateFoo') outside of any $apply
block. Suppose an AngularJS based controller [[B]] also calls
$rootScope.$broadcast('updateFoo') in a click handler, so it is within an
existing $apply block.
Now suppose a 2nd $scoped component [[C]] wants to handle
$on('updateFoo'), updating a $scope value that affects the rendering of
[[CC]]. If [[C]]'s event handler does not call $apply, then there will not
be a digest cycle updating the components rendering when [[A]] broadcasts.
If [[C]]'s event handler does call $apply, an error will be thrown when
[[B]] broadcasts that event, because the broadcast happens within another
$apply context. The error message is something like '$apply already in
progress'.
So how should [[C]] deal with $apply when modifying $scope? I feel like
I'm doing something wrong, there is poor separation of concerns if [[C]]
has to consider whether [[A]] or [[B]] broadcast the event.
I have seen some safe $apply implementations suggested online. This
feels wrong though, the fact that I need to bypass the frameworks behavior
makes me think I am misunderstanding something. I am on Angular 1.2.14.
Thanks.
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.