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.4.0/angular.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body ng-app='demoModule'>
  <div ng-controller='demoContollerOne'>
    <input type="text" ng-model="oneInput">
    <button type='button' ng-click="oneFunction()">copy</button>
  </div>
  <div ng-controller='demoContollerTwo'>
    <input type="text" ng-model="twoInput">
    <button type='button' ng-click="twoFunction()">copy</button>
  </div>
</body>
</html>
 
var demoModule = angular.module("demoModule", []);
demoModule.factory("demoService", function() {
  var demoSharedVariable = null;
  return {
    demoSharedVariable: demoSharedVariable
  }
});
demoModule.controller("demoContollerOne", function($scope, demoService) {
  $scope.oneFunction = function(){
    demoService.demoSharedVariable = $scope.oneInput;
  };
  
  $scope.$watch(function(){
    return demoService.demoSharedVariable;
  }, function(){
    $scope.oneInput = demoService.demoSharedVariable;
  });
});
demoModule.controller("demoContollerTwo", function($scope, demoService) {
  $scope.twoFunction = function(){
    demoService.demoSharedVariable = $scope.twoInput;
  };
  
  $scope.$watch(function(){
    return demoService.demoSharedVariable;
  }, function(){
    $scope.twoInput = demoService.demoSharedVariable;
  });
});
Output

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

Dismiss x
public
Bin info
anonymouspro
0viewers