Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.min.js"></script>
  
  <script>
    var app = angular.module('myApp', []);
    app.controller('MainController', function($scope, sharedService) {
    
    // Start broadcast
    $scope.runIt = function(){
      sharedService.prepForBroadcast('Hello AngularJS');
    };
});
app.controller('SecondController', function($scope) {
    // This piece of code doesn't get called.
    $scope.$on('handleBroadcast', function() {
        alert('received broadcast');
    });
});
app.factory('sharedService', function($rootScope) {
    var data = {message: ''};
    data.prepForBroadcast = function(message) {
        this.message = message;
        $rootScope.$broadcast('handleBroadcast');
    };
    return data;
}); 
  </script>
  </head>
<body ng-app='myApp'>
  
  <div ng-controller="MainController">
    Main <button ng-click="runIt()">Run it</button>
  </div>
  
  <div ng-controller="SecondController">
   Second
  </div>
</body>
</html>
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers