Humberto
Humberto

Reputation: 7199

AngularJs: Applying scope changes from event firing

I have a directive which encapsulates a dialog box, and it's almost completely isolated from the surrounding scopes: the only way to communicate with/from it is by firing events. For instance, dialog box commands such as "Apply", "Save", "Delete" or "Turn the lights off" are issued by clicking buttons and firing corresponding events which other scopes may be waiting for.

As we don't know which scopes are registered to handle any specific events, I've resorted to broadcast the dialog's command events all the way up from $rootScope, and of course it works. However, if an event handler changes the model, the view isn't updated, because there's no $apply or $digest following the event firing.

enter image description here

Here lies my dilemma: where to call $digest from? Or am I very misguided about the whole thing?

Upvotes: 1

Views: 572

Answers (1)

Mark Rajcok
Mark Rajcok

Reputation: 364697

Since $broadcast is synchronous, I suggest that the code that is calling $broadcast should call $rootScope.$apply() after calling $broadcast, if that code is running "outside" AngularJS. (If that code is running "inside" Angular, $apply() should not be needed.)

Upvotes: 1

Related Questions