1

I have a code which works for both factory and service : I am confused at what circumstances I need to use which code.

This code output my value in console.log using service

var mod = angular.module("MyModule", []);

mod.service("myService", function() {
  console.log("this is service");
    return {
      getvalue : function() {
        return "My value";
      }
  };
});

mod.controller("MyController", function(myService) {
  console.log("MyController - myFactory: " + myService.getvalue());
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="MyModule">
<div ng-controller="MyController"></div>
</div>

This code I have just change mod.service() to mod.factory() and rest of code is same and it output's my value in console.log using factory

var mod = angular.module("MyModule", []);

mod.service("myFactory", function() {
  console.log("this is Factory");
    return {
      getvalue : function() {
        return "My value";
      }
  };
});

mod.controller("MyController", function(myFactory) {
  console.log("MyController - myFactory: " + myFactory.getvalue());
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="MyModule">
<div ng-controller="MyController"></div>
</div>
RJV
  • 287
  • 1
  • 7
  • 20
  • If the function creates and returns an object (the service), then it's a factory and you should use factory(). If the function is a constructor function initializing `this` and not returning anything, then use service(). You can actually use service with a function returning a service, but that's only because JavaScript constructors can do ugly things. – JB Nizet Oct 27 '16 at 06:26
  • when we use factory, we just create an object, adding properties to it, then return that same object.When we pass this factory into controller, those properties on the object will now be available in that controller.If using an object, we can use the factory provider. when we use Service , we just instantiated with the ‘new’ keyword and you’ll add properties to ‘this’ and the service will return ‘this’. When we pass the service into controller, those properties on ‘this’ will now be available on that controller.service is better if using with class – Nikhil Mohanan Oct 27 '16 at 06:39
  • 1
    Possible duplicate of [AngularJS: Service vs provider vs factory](http://stackoverflow.com/questions/15666048/angularjs-service-vs-provider-vs-factory) – Nasreddine Oct 27 '16 at 06:54
  • Possible duplicate of [Creating common controller functions](http://stackoverflow.com/questions/11324202/creating-common-controller-functions) – Mistalis Oct 27 '16 at 06:56

2 Answers2

2

This blog post by Todd Motto is worth a thousands answers.

Using .factory() gives us much more power and flexibility, whereas a .service() is essentially the “end result” of a .factory() call. The .service() gives us the returned value by calling new on the function, which can be limiting, whereas a .factory() is one-step before this compile process as we get to choose which pattern to implement and return.

It explains what is the difference at the source code level.

In real life, my opinion is that it's a matter of taste to use one or the other.

Coming from Java, I like the fact that .service looks like a class with the use of this.

gyc
  • 4,300
  • 5
  • 32
  • 54
1

Let me explain

Services

Services provide a method for us to keep data around for the lifetime of the app and communicate across controllers in a consistent manner. Services are singleton objects that are instantiated only once per app (by the $injector) and lazyloaded (created only when necessary). They provide an interface to keep together those methods that relate to a specific function.

Factory

This service factory function is responsible for generating a single object or function that becomes this service, which will exist for the lifetime of the app. When our Angular app loads the service, the service will execute this function and hold on to the returned value as the singleton service object. The service factory function can be either a function or an array, just like the way we create controllers:

So i would like you to use factory for your core operations, and call this factory from one service(You need to create one saporate services for this), so this way you can provide more isolation in your app.

Example

*make one factory like :

app.factory('memberServiceFactory', ['$http','$filter', function ($http,$filter) {
    return {
        getData: function (data) {
            return {};
        },           
        addData: function (file, formData) {
        },
    }
}]);

*make one service like :

app.service('memberListService', ['memberServiceFactory', function (memberServiceFactory){
        this.getList = memberServiceFactory.getData;
        this.addMember = memberServiceFactory.addData;               
 }]);

You can see here i am calling factory from services, you can provide encapsulation like this.

Jigar7521
  • 1,549
  • 14
  • 27
  • You're not calling a factory from a service. You're calling a service from another service. factory() and service() are just two different ways of defining a service. What is injected by angular is the service, not the factory that was used to create it. Also, this seems like quotes from the documentation or some other page, so you should make that clear and include a reference (link) to the quoted material. – JB Nizet Oct 27 '16 at 06:50
  • no i am calling factory from services, there are i have just given name like the services if factory you can see – Jigar7521 Oct 27 '16 at 06:51
  • You can check now, there was just naming standards issue. – Jigar7521 Oct 27 '16 at 06:52
  • No, you are not. The factory is `function ($http,$filter) { ... }`. That's not what is injected and called by the memberListService. What is injected and called is the object created and returned by this factory function: the service. – JB Nizet Oct 27 '16 at 06:53