I have a footerController and codeScannerController with different views.
angular.module(\'myApp\').controller(\'footerController\', [\"$scope\", function($s
First, a short description of $on(), $broadcast() and $emit():
.$on(name, listener) - Listens for a specific event by a given name.$broadcast(name, args) - Broadcast an event down through the $scope of all children.$emit(name, args) - Emit an event up the $scope hierarchy to all parents, including the $rootScopeBased on the following HTML (see full example here):
The fired events will traverse the $scopes as follows:
$scope$scope then $rootScope$scope then Controller 3 $scope$scope then $rootScope$scope$scope, Controller 2 $scope then $rootScope$rootScope and $scope of all the Controllers (1, 2 then 3) $rootScopeJavaScript to trigger events (again, you can see a working example here):
app.controller('Controller1', ['$scope', '$rootScope', function($scope, $rootScope){
$scope.broadcastAndEmit = function(){
// This will be seen by Controller 1 $scope and all children $scopes
$scope.$broadcast('eventX', {data: '$scope.broadcast'});
// Because this event is fired as an emit (goes up) on the $rootScope,
// only the $rootScope will see it
$rootScope.$emit('eventX', {data: '$rootScope.emit'});
};
$scope.emit = function(){
// Controller 1 $scope, and all parent $scopes (including $rootScope)
// will see this event
$scope.$emit('eventX', {data: '$scope.emit'});
};
$scope.$on('eventX', function(ev, args){
console.log('eventX found on Controller1 $scope');
});
$rootScope.$on('eventX', function(ev, args){
console.log('eventX found on $rootScope');
});
}]);