AngularJS : Factory and Service?

This question already has an answer here:

  • AngularJS: Service vs provider vs factory 30 answers

  • Service vs Factory


    在这里输入图像描述在这里输入图像描述

    The difference between factory and service is just like the difference between a function and an object

    Factory Provider

  • Gives us the function's return value ie. You just create an object, add properties to it, then return that same object.When you pass this service into your controller, those properties on the object will now be available in that controller through your factory. (Hypothetical Scenario)

  • Singleton and will only be created once

  • Reusable components

  • Factory are a great way for communicating between controllers like sharing data.

  • Can use other dependencies

  • Usually used when the service instance requires complex creation logic

  • Cannot be injected in .config() function.

  • Used for non configurable services

  • If you're using an object, you could use the factory provider.

  • Syntax: module.factory('factoryName', function);

  • Service Provider

  • Gives us the instance of a function (object)- You just instantiated with the 'new' keyword and you'll add properties to 'this' and the service will return 'this'.When you pass the service into your controller, those properties on 'this' will now be available on that controller through your service. (Hypothetical Scenario)

  • Singleton and will only be created once

  • Reusable components

  • Services are used for communication between controllers to share data

  • You can add properties and functions to a service object by using the this keyword

  • Dependencies are injected as constructor arguments

  • Used for simple creation logic

  • Cannot be injected in .config() function.

  • If you're using a class you could use the service provider

  • Syntax: module.service('serviceName', function);

  • Sample Demo

    In below example I have define MyService and MyFactory . Note how in .service I have created the service methods using this.methodname. In .factory I have created a factory object and assigned the methods to it.

    AngularJS .service


    module.service('MyService', function() {
    
        this.method1 = function() {
                //..method1 logic
            }
    
        this.method2 = function() {
                //..method2 logic
            }
    });
    

    AngularJS .factory


    module.factory('MyFactory', function() {
    
        var factory = {}; 
    
        factory.method1 = function() {
                //..method1 logic
            }
    
        factory.method2 = function() {
                //..method2 logic
            }
    
        return factory;
    });
    

    Also Take a look at this beautiful stuffs

    Confused about service vs factory

    AngularJS Factory, Service and Provider

    Angular.js: service vs provider vs factory?


    Factory and Service is a just wrapper of a provider .

    Factory

    Factory can return anything which can be a class(constructor function) , instance of class , string , number or boolean . If you return a constructor function, you can instantiate in your controller.

     myApp.factory('myFactory', function () {
    
      // any logic here..
    
      // Return any thing. Here it is object
      return {
        name: 'Joe'
      }
    }
    

    Service

    Service does not need to return anything. But you have to assign everything in this variable. Because service will create instance by default and use that as a base object.

    myApp.service('myService', function () {
    
      // any logic here..
    
      this.name = 'Joe';
    }
    

    Actual angularjs code behind the service

    function service(name, constructor) {
        return factory(name, ['$injector', function($injector) {
            return $injector.instantiate(constructor);
        }]);
    }
    

    It just a wrapper around the factory . If you return something from service , then it will behave like Factory .

    IMPORTANT : The return result from Factory and Service will be cache and same will be returned for all controllers.

    When should i use them?

    Factory is mostly preferable in all cases. It can be used when you have constructor function which needs to be instantiated in different controllers.

    Service is a kind of Singleton Object. The Object return from Service will be same for all controller. It can be used when you want to have single object for entire application. Eg: Authenticated user details.

    For further understanding, read

    http://iffycan.blogspot.in/2013/05/angular-service-or-factory.html

    http://viralpatel.net/blogs/angularjs-service-factory-tutorial/


  • If you use a service you will get the instance of a function ("this" keyword).
  • If you use a factory you will get the value that is returned by invoking the function reference (the return statement in factory)
  • Factory and Service are the most commonly used recipes. The only difference between them is that Service recipe works better for objects of custom type, while Factory can produce JavaScript primitives and functions.

    Reference

    链接地址: http://www.djcxy.com/p/2192.html

    上一篇: 如何查找不包含给定字符串模式的文件?

    下一篇: AngularJS:工厂和服务?