Skip to content

Instantly share code, notes, and snippets.

@GrahamWalters
Created April 11, 2015 12:17
Show Gist options
  • Save GrahamWalters/68614da03d19ee97ea0a to your computer and use it in GitHub Desktop.
Save GrahamWalters/68614da03d19ee97ea0a to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/hamibiwura
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body ng-app="app">
<div ng-controller="Ctrl1"></div>
<div ng-controller="Ctrl2"></div>
<script id="jsbin-javascript">
console.log('A service wraps a factory.');
console.log('All providers in Angular are singletons.');
var app = angular.module('app', []);
app.factory('MatchFactory', function() {
var Match = function() {
console.log('MatchFactory');
this.name = '';
this.setName = function(name) {
this.name = name;
};
this.hello = function() {
console.log('Hello '+this.name);
};
};
Match.prototype.helloP = function() {
console.log('HelloP '+this.name);
};
return new Match();
});
app.service('MatchService', function() {
console.log('MatchService');
this.name = '';
this.setName = function(name) {
this.name = name;
};
this.hello = function() {
console.log('Hello '+this.name);
};
});
app.controller('Ctrl1', function(MatchFactory, MatchService) {
MatchFactory.setName('Ben');
MatchService.setName('Jerry');
});
app.controller('Ctrl2', function(MatchFactory, MatchService) {
MatchFactory.hello();
MatchFactory.helloP(); // prototype functions also work
MatchService.hello();
});
</script>
<script id="jsbin-source-html" type="text/html"><!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.js"><\/script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body ng-app="app">
<div ng-controller="Ctrl1"></div>
<div ng-controller="Ctrl2"></div>
</body>
</html></script>
<script id="jsbin-source-javascript" type="text/javascript">console.log('A service wraps a factory.');
console.log('All providers in Angular are singletons.');
var app = angular.module('app', []);
app.factory('MatchFactory', function() {
var Match = function() {
console.log('MatchFactory');
this.name = '';
this.setName = function(name) {
this.name = name;
};
this.hello = function() {
console.log('Hello '+this.name);
};
};
Match.prototype.helloP = function() {
console.log('HelloP '+this.name);
};
return new Match();
});
app.service('MatchService', function() {
console.log('MatchService');
this.name = '';
this.setName = function(name) {
this.name = name;
};
this.hello = function() {
console.log('Hello '+this.name);
};
});
app.controller('Ctrl1', function(MatchFactory, MatchService) {
MatchFactory.setName('Ben');
MatchService.setName('Jerry');
});
app.controller('Ctrl2', function(MatchFactory, MatchService) {
MatchFactory.hello();
MatchFactory.helloP(); // prototype functions also work
MatchService.hello();
});</script></body>
</html>
console.log('A service wraps a factory.');
console.log('All providers in Angular are singletons.');
var app = angular.module('app', []);
app.factory('MatchFactory', function() {
var Match = function() {
console.log('MatchFactory');
this.name = '';
this.setName = function(name) {
this.name = name;
};
this.hello = function() {
console.log('Hello '+this.name);
};
};
Match.prototype.helloP = function() {
console.log('HelloP '+this.name);
};
return new Match();
});
app.service('MatchService', function() {
console.log('MatchService');
this.name = '';
this.setName = function(name) {
this.name = name;
};
this.hello = function() {
console.log('Hello '+this.name);
};
});
app.controller('Ctrl1', function(MatchFactory, MatchService) {
MatchFactory.setName('Ben');
MatchService.setName('Jerry');
});
app.controller('Ctrl2', function(MatchFactory, MatchService) {
MatchFactory.hello();
MatchFactory.helloP(); // prototype functions also work
MatchService.hello();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment