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.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body ng-app="app">
  <div ng-controller="MyController">
    Markers from MyController: <br />{{markers}}
  </div>
  
  <div ng-controller="AnotherController">
    Inserting markers from AnotherController<br />
    <input type="text" ng-model="markerText" /><button ng-click="addMarker(markerText)">Add Marker</button>
  </div>
</body>
</html>
 
var app = angular.module('app', []);
app.factory('MapService', function() {
  var observerCallbacks = [];
  var markers = [];
  var publicMembers = {
    addMarker: addMarker,
    getMarkers: getMarkers,
    registerObserverCallback: registerObserverCallback
  }
  function addMarker (marker) {
     markers.push(marker);
     notifyObservers();
  };
  function getMarkers() {
     return markers;
  }
  function registerObserverCallback(callback){
    observerCallbacks.push(callback);
  };
  function notifyObservers(){
    angular.forEach(observerCallbacks, function(callback){
      callback();
    });
  };
  return publicMembers;
});
app.controller('MyController', function($scope, MapService) {
    MapService.registerObserverCallback(function() {
        $scope.markers = MapService.getMarkers();
    });
});
app.controller('AnotherController', function($scope, MapService) {
  
  $scope.addMarker = MapService.addMarker;
});
Output

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

Dismiss x
public
Bin info
anonymouspro
0viewers