In my AngularJS app, I have three controllers. One is the main controller and the other two are siblings.
I have Sibling control 1
to emit data to Main control
, which broadcasts the data, which sibling control 2
then picks up.
Sibling control 1
$scope.selectedPatentFx;
$scope.$watch('selectedPatentFx', function(newValue, oldValue){
if($scope.selectedPatentFx) {
$scope.$emit('calculateFx', {patentfx: newValue});
}
})
Main control
$scope.$on('calculateFx', function(event, obj){
$scope.$broadcast('calculateFxBroadcast', {fx: obj})
});
Sibling control 2
$scope.$on('calculateFxBroadcast', function(event, obj){
//handle obj
})
The issue is that the data is being sent twice. However it doesn't cause any errors (as of yet).
Question
Why is the data being emitted/broadcasted twice?
I would avoid using events (
$broadcast
) here. You can do it by using a service which shares the data. I created an abstract example which delivers you the basic handling.> Share data via service between controllers - demo fiddle
View
AngularJS application