anilkumar
anilkumar

Reputation: 21

what is main purpose of broadcast in angularJS?

I saw some examples for broadcast there I observed it is for send data from parent controller to child controller only, but by default child control hava access to the parent controller so what is specific purpose of it? please do not bring rootscope, if we use rootscope then no need of broadcast and emit

Upvotes: -1

Views: 353

Answers (2)

Gabriel Lovetro
Gabriel Lovetro

Reputation: 410

If you are not using $rootScope, then you'd need to name each $scope.$on() callback function you used to receive the event emitted by the original $broadcast, unless you only have one receiver. If you do not name the callback functions, only one $scope.$on will work (the last to be "created", as it will overwrite the previous ones).

So, by having several receivers for one emitter you can, for example, sync several views. Say you change the variable age of a user in one page (where it can be edited) and three other pages need to display that updated variable - you could $broadcast it to all 3 pages.

Upvotes: 0

Suman M R
Suman M R

Reputation: 1

It's not only having access to data. In some cases when you have to perform some actions (in parent controller) depending on the events triggered (in the child controller) you can make use of broadcast.

Upvotes: 0

Related Questions