Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  
   <div id="1">
     <div ng-controller="c1">
       <button ng-click="updateData('asdf')">add</button>
       {{data}}
     </div>
   </div>
  
   <hr>
  
   <div id="2">
     <div ng-controller="c2">
       <button ng-click="updateData('qwer')">add</button>
       {{data}}
     </div>     
   </div>
</body>
</html>
 
var app1 = angular.module('app1', []);
var app2 = angular.module('app2', []);
function SharedCommunicatorThingy () {
  var scopes = [];
  var sharedData   = [];
  this.add = function (v) {
    sharedData.push(v);
    scopes.forEach(function (s) {
      s.$evalAsync();
    });
  };
  this.attach = function (scope) {
    scopes.push(scope);
  };
  
  Object.defineProperty(this, 'data', {
    get: function () {
      return sharedData;
    }
  });
}
window.shared = new SharedCommunicatorThingy();
function controller ($scope, $window) {
  $window.shared.attach($scope);
  $scope.updateData = function (arg) {
    $window.shared.add(arg);
  };
  
  $scope.data = $window.shared.data;  
}
angular.module('app1').controller('c1', controller);
angular.module('app2').controller('c2', controller);
angular.bootstrap(document.getElementById('1'), ['app1']);
angular.bootstrap(document.getElementById('2'), ['app2']);
Output

This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account

Dismiss x
public
Bin info
anonymouspro
0viewers