I have been coding in Angular for a little bit of time now and I have created Modules, Controller and Services so far but I recently just came across a piece of code which I am just not able to understand. It is someone else's code and I think author has tried to define a service but not like I have created. Normally when I create a service it looks like below:
app.factory('portabilityService', [
'$resource', '$state', '$rootScope', function ($resource, $state, $rootScope) {
return { //definition };
}
]);
- To me it is simple specify service name as the first argument inside Factory followed by all the dependencies and there you go.
Now, I came across some other code which looks like following:
(function () {
/*
* Manages which samples have been selected
*/
'use strict';
angular.module('maintenance.portability.module')
.factory('selectedSamplesSvc', service);
service.$inject = [];
function service()
{
//other function definitions
};
})();
Now, this raises a lot of questions:
- First of all, is this really a service?
- If yes, what is service.$inject = []?
- Where should I add the dependencies?
- What is the purpose of the constructor service() here?
- Is it just a different way to create a service or there is a specific reason why we should define a service in this particular way?