I currently have an Angular controller that is quite simple:
angular.controller('appCtrl', function ($scope, $rootScope) {
$rootscope.$on('channel.message', function () {
// do something here
}
});
In addition, I have a sidebar on my webpage that directs me to a view associated with the mentioned controller.
The problem arises when each time I click on a link, Angular creates a new instance of the controller. While this is acceptable, I've noticed that the number of subscribers for my 'channel.message'
event is continually increasing, which is not ideal.
I am aware that each piece of code simply adds another callback to the queue but I would like to find a way to only have a single subscriber. What are the recommended best practices in this scenario?
Additionally, I'm familiar with using $scope.$on but it's not suitable due to performance implications and the architecture design of the application itself.