Most of the articles talk about differences.I will start with similarities and work our way to differences.
I will be using the code added https://github.com/abhikmitra/Service-vs-Factory/tree/master/app/scripts .
So firstly we have 3 pages with 3 separate controllers.
1.MainCtrl,
2.AnotherCtrl,
3.OnemoreCtrl
Claim 1 : Services and factory are both lazy loaded.
Proof : Move from “another page” to “main page” and only then you will see customFactory, Customservice getting instantiated.
Claim 2: Both of them are singleton and can be accessed in the same way
ServiceOrFactorName.somefunction();
Proof:
Our Service look like
angular.module('serviceVsFactoryApp')
.service('Customservice', function Customservice() {
console.log("CustomService has been Called");
console.warn("Value of this inside a factory",this,"Notice the __proto__ by putting a break point")
var localVariable = 1;
this.thisVariable = 100;
this.test = function () {
return ++localVariable;
}
});
This allows us to call the service from controller with the following syntax.
</pre>
Customservice.test();
<pre>
So what does angular internally do ? It does a new of the contructor function Customservice() and caches it for it to be used as a singleton .If you put a breakpoint in the customservice and check the value of this you will see
this.__proto__ = Customservice
Now if we want the same functionality form the factory,this is what we will be doing.
</pre>
angular.module('serviceVsFactoryApp')
.factory('customFactory', function () {
console.log("CustomFactory has been Called")
var localVariable = 1;
console.warn("Value of this inside a factory",this,"Notice the $get")
return {
test: function () {
console.warn("Value of this inside the object a factory",this,"Notice the __proto__")
return ++localVariable;
},
thisVariable : 100
};
})
<pre>
So for the first time when the service is instantiated customFactory function is invoked once. and the returned object is cached for the lifetime of the app.Hence we can access the test function in the same was as previously.
Putting a break point in the code ,you will see that the this operator has a $get which basically means that the factory function is the function that we pass it to the provider using $get.
Another way to achieve the same goal is through this way
</pre>
angular.module('serviceVsFactoryApp')
.factory('customFactoryThatInstantiatesAfunction', function () {
console.log("CustomFactory has been Called")
var localVariable = 1;
console.warn("Value of this inside a factory",this,"Notice the $get")
function Customservice() {
console.log("CustomService has been Called");
console.warn("Value of this inside a factory", this, "Notice the __proto__ by putting a break point")
var localVariable = 1;
this.thisVariable = 100;
this.test = function () {
return ++localVariable;
}
}
return new Customservice();
})
<pre>
This is exactly what a service does.So a service is a syntactic sugar on top of factory which itself is a syntactic sugar on providers
Some of the similarities.
Both the service and factory can have private variables in our case we call it local variables.
In your controller ,you can choose to do this
</pre>
<pre>customFactory.addingRandom="random text"; Customservice.addingRandom="random text";</pre>
<pre>
Which basically adds a prototype to the current instance.And since its a singleton,all the modules using this instance get access to the value.
So this brings the part 1 to close. In part 2 we will see more of differences than similarities.
22.572646
88.363895