Looking to dispatch an event to another controller, I am familiar with techniques involving $rootScope.$broadcast
, $scope.$emit
, and then listening for the event using $scope.$on
.
This is a common approach, but in my project, controllers are initialized in separate JavaScript files.
//In broadcaster.js
function broadcasterCtrl(~~~~~, broadcasterService){
~~~~~~~~~~
}
function broadcasterService(~~~~~){
~~~~~~~~~~
}
angular
.module('myApp')
.service('broadcasterService', broadcasterService)
.controller('broadcasterCtrl', broadcasterCtrl);
//In listener.js
function listenerCtrl(~~~~~, listenerService){
~~~~~~~~~~
}
function listenerService(~~~~~){
~~~~~~~~~~
}
angular
.module('myApp')
.service('listenerService', listenerService)
.controller('listenerCtrl', listenerCtrl);
Due to this setup, the listener controller is initialized when the view(state) is called. As a workaround, I used
$window.localStorage.setItem('key', value)
, although I have concerns about its security vulnerabilities.
Are there alternative approaches utilizing $broadcast
? Or is it safe to rely on localStorage?