I have two controllers in a simple AngularJS app that need to communicate data between one another.
I am trying to use events to perform this communication. I have tried using both $scope.$emit and $scope.$broadcast to trigger the change and $scope.$on to consume the events.
Broadcasting events via either $broadcast or $emit does not seem to result in the resulting $on method being fired
Sample, non-working, of what I am trying to achieve is below.
var app = angular.module('angular-app', []);
app.controller('TaskCtrl', function ($scope) {
$scope.setSelected = function (idSelectedVote) {
$scope.$broadcast("taskRowSelected");
};
});
app.controller('WorkRecordCtrl', function ($scope) {
$scope.$on('taskRowSelected', function (event, data) {
console.log(data); // 'Data to send'
console.log("Some Data");
});
});
Is there a way to achieve direct communication between two controllers at the same level using events?
If not what would be the best way to facilitate the communication?