I am trying to trigger an event from a parent controller and the child controller should listen to it.Using angular's event framework there are two ways to go
Method 1
parentcontroller
$scope.$broadcast("eventToChild");
childController
$scope.$on("eventToChild",function({console.log("received event");})
Method 2
parentcontroller
$rootScope.$emit("eventToChild");
childController
$rootScope.$on("eventToChild",function({console.log("received event");})
I understand that $rootscope.$emit
will only invoke listeners on $rootscope
and we can stop the propagation of the event using event.stopPropagation()
,given this fact how can $scope.$broadcast
be a better solution than $rootScope.$emit
.
Which one among these two is a better solution and why?