Factory vs Service
angularjs factory vs service do the same thing, just a different way of instantiation
by BinaryMuse
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.factory('myKlass', function() {
return Klass
});
function CtrlA($scope, myKlass)
{
// var a = new Klass();
// window.alert(a.getAndIncCount());
// var b = new Klass();
// window.alert(b.getAndIncCount());
$scope.fromService = new myKlass().getAndIncCount();
}
function CtrlB($scope, myKlass)
{
// I want an independant copy of testFactory
$scope.fromService = new myKlass().getAndIncCount();
}