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.3.2/angular.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body ng-app="myApp">
  <div ng-controller="myCtrl"></div>
  <div ng-controller="myCtrl2"></div>
</body>
</html>
 
angular.module('myApp', [])
.factory('myService', function($http) {
  var cachedPromises = {};
  return {
    getStuff: function (id) {
      if (!cachedPromises[id]) {
        cachedPromises[id] = $http.post('/').then(function(resp) {
          return {foo:'foo val', bar: 'bar val'}; //mock data
        });
      }
      return cachedPromises[id];
    }
};
})
.controller('myCtrl', function(myService) {
  myService.getStuff(1).then(function(data) {
    console.log(data.foo);
    data.foo = 'abc';
  });
})
.controller('myCtrl2', function(myService) {
  myService.getStuff(1).then(function(data) {
    console.log(data.foo);
  });
})
;
Output

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

Dismiss x
public
Bin info
m59peacemakerpro
0viewers