Factory vs Service
angularjs factory vs service do the same thing, just a different way of instantiation
HTML
<script src="http://code.angularjs.org/angular-1.0.0rc9.js"></script>
<div ng-controller="CtrlA">
<p>Count - {{fromService}}</p>
</div>
<br />
<div ng-controller="CtrlB">
I'd like for this to display '1' not '2'
<p>Count - {{fromService}}</p>
</div>
JavaScript
var app = angular.module('myApp', []);
var Klass = function() {
this.count = 0;
this.getAndIncCount = function() {
this.count += 1;
return this.count;
};
};
app.service('service', Klass);
function CtrlA($scope, service)
{
// var a = new Klass();
// window.alert(a.getAndIncCount());
// var b = new Klass();
// window.alert(b.getAndIncCount());
$scope.fromService = service.getAndIncCount();
}
function CtrlB($scope, service)
{
// I want an independant copy of testFactory
$scope.fromService = service.getAndIncCount();
}