Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$rootScope.$broadcast vs. $scope.$emit

Now that the performance difference between $broadcast and $emit has been eliminated, is there any reason to prefer $scope.$emit to $rootScope.$broadcast?

They are different, yes.

$emit is restricted to the scope hierarchy (upwards) - this may be good, if it fits your design, but it seems to me a rather arbitrary restriction.

$rootScope.$broadcast works across all that choose to listen to the event, which is a more sensible restriction in my mind.

Am I missing something?

EDIT:

To clarify in response to an answer, the direction of the dispatch is not the issue I'm after. $scope.$emit dispatches the event upwards, and $scope.$broadcast - downwards. But why not always use $rootScope.$broadcast to reach all the intended listeners?

like image 362
New Dev Avatar asked Nov 05 '14 07:11

New Dev


People also ask

What is the difference between rootScope and scope?

We can say that $rootscope is available to all the controllers whereas $scope is available only to the controller that created it.

What does rootScope broadcast do?

The $rootScope. $broadcast is used to broadcast a “global” event that can be caught by any listener of that particular scope. The descendant scopes can catch and handle this event by using $scope.

Is there no equivalent to scope emit () or scope Broadcast () in Angular?

Both $broadcast() and $emit() allow you to raise an event in your AngularJS application, using the event system of $scope and $rootScope . Again, there is no equivalent to this in modern Angular. If you are looking for a permanent fix during your migration to Angular, you will need an architectural level solution.

What is the difference between $emit and broadcast?

The difference between $broadcast() and $emit() is that the former sends the event from the current controller to all of its child controllers. That means $broadcast() sends an even downwards from parent to child controllers. The $emit() method, on the other hand, does exactly opposite.


1 Answers

tl;dr (this tl;dr is from @sp00m's answer below)

$emit dispatches an event upwards ... $broadcast dispatches an event downwards

Detailed explanation

$rootScope.$emit only lets other $rootScope listeners catch it. This is good when you don't want every $scope to get it. Mostly a high level communication. Think of it as adults talking to each other in a room so the kids can't hear them.

$rootScope.$broadcast is a method that lets pretty much everything hear it. This would be the equivalent of parents yelling that dinner is ready so everyone in the house hears it.

$scope.$emit is when you want that $scope and all its parents and $rootScope to hear the event. This is a child whining to their parents at home (but not at a grocery store where other kids can hear).

$scope.$broadcast is for the $scope itself and its children. This is a child whispering to its stuffed animals so their parents can't hear.

like image 55
Eddie Monge Jr Avatar answered Oct 06 '22 13:10

Eddie Monge Jr