Why do we use $rootScope.$broadcast in AngularJS?

$rootScope basically functions as an event listener and dispatcher. To answer the question of how it is used, it used in conjunction with rootScope.$on; $rootScope.$broadcast(“hi”); $rootScope.$on(“hi”, function(){ //do something }); However, it is a bad practice to use $rootScope as your own app’s general event service, since you will quickly end up in a situation … Read more

How do I use $rootScope in Angular to store variables?

Variables set at the root-scope are available to the controller scope via prototypical inheritance. Here is a modified version of @Nitish’s demo that shows the relationship a bit clearer: http://jsfiddle.net/TmPk5/6/ Notice that the rootScope’s variable is set when the module initializes, and then each of the inherited scope’s get their own copy which can be … Read more