What I want is for SubCtrl to broadcast an event when it is loaded saying that it wants to get information. I want MainCtrl to be listening for a call to the get event and respond by broadcasting the 'set' event and providing its name property as an argument. The SubCtrl should be listening for the set event and respond by populating its own scopes name property.
With the following code, MainCtrl receives the get event, but SubCtrl never receives the set event.
Script
app.controller('MainCtrl', function($scope, $rootScope) {
$scope.name = 'World';
$scope.$on('get', function(event) {
console.log('get event received');
$rootScope.$broadcast('set', $scope.name);
})
});
app.controller('SubCtrl', function($scope, $rootScope) {
$scope.name = '..waiting';
$rootScope.$broadcast('get');
$scope.$on('set', function(event, name) {
console.log('set event received');
$scope.name = name;
});
});
Markup
<body>
<p ng-controller="MainCtrl">Hello {{name}}!</p>
<p ng-controller="SubCtrl">Hello {{name}}!</p>
</body>