Search code examples
angularjsangularjs-directivecontrollerbroadcast

I want handle two independent controllers without broadcasting $rootScope in AngularJS


I did that with broadcast event into $rootScope. Now I am trying to do that without using $rootScope. Is that possible ?

Here is the code can check what i did with $rootScope

Example code here


Solution

  • To share data between controllers using events always needs a scope that's up the hierarchy of both controller's scopes, because broadcast events bubble down the scope hierarchy from parent to children. Because $rootScope is the topmost scope it is save to use it for broadcasting. The alternative solution would be to add watch functions to both controllers watching a variable in an injected service. Because services are singletons this would be fine too.