I am facing numerous situations where I require clicks or other interactions to trigger actions in a different area of the webpage (one-way communication). Now, I have encountered a need for bidirectional communication, where changes made in element A can affect specific attributes in the context behind element B and vice versa. Up until now, I have been using $rootScope.$broadcast to achieve this functionality, but it seems excessive and results in redundant code in both places:
$scope.$on('event-name', function(event, someArg) {
if(someArg === $scope.someProperty) return;
$scope.someProperty = someArg;
});
$scope.$watch('someProperty', function(newValue) {
$rootScope.$broadcast('event-name', newValue);
});
Is there a more efficient solution? I would like to connect the two (or three, or N) scopes through a service, but I cannot find a way to do so without resorting to magical event names and repetitive code.