2

In other words, is it true that I have to wait for the next event cycle to broadcast using $rootScope.broadcast(), so that other controllers using $rootScope.on() can pick up the broadcasted event?

It is not the usual app flow, but let's say if we have a demo mode in the app which would short circuit some logic, and not need to go the server to fetch something, but can provide the demo data using the broadcast(), is it true that we have to use

setTimeout(function() { 
    $rootScope.broadcast(...);
}, 0);

so that other controllers will pick it up?

The reason is, other controllers might not exist yet. If the app module defines 5 controllers, or use dependency injection to declare it depends on 2 sets of controllers, abc.xyz.controllers and abc.efg.controllers.

Since a controller definition is

.controller("name", function(...) {

})

so by providing the function, you don't know when the function is called (to create the controller), but assuming all creations were done in the first event cycle, then if you wait one cycle to broadcast, then you can be sure that all controllers will pick up the broadcast? Is this formally documented too?

nonopolarity
  • 146,324
  • 131
  • 460
  • 740
  • Even I am curious to know as I see a lot of examples using this. It could be true what you said. – Saksham Mar 24 '16 at 06:29

0 Answers0