Confused about Service vs Factory

后端 未结 20 2444
轻奢々
轻奢々 2020-11-22 08:49

As I understand it, when inside a factory I return an object that gets injected into a controller. When inside a service I am dealing with the object using this

20条回答
  •  花落未央
    2020-11-22 09:42

    There are three ways of handling business logic in AngularJS: (Inspired by Yaakov's Coursera AngularJS course) which are:

    1. Service
    2. Factory
    3. Provider

    Here we are only going to talk about Service vs Factory

    SERVICE:

    Syntax:

    app.js

     var app = angular.module('ServiceExample',[]);
     var serviceExampleController =
                  app.controller('ServiceExampleController', ServiceExampleController);
     var serviceExample = app.service('NameOfTheService', NameOfTheService);
    
     ServiceExampleController.$inject = ['NameOfTheService'] //very important as this protects from minification of js files
    
    function ServiceExampleController(NameOfTheService){
         serviceExampleController = this;
         serviceExampleController.data = NameOfTheService.getSomeData();
     }
    
    function NameOfTheService(){
         nameOfTheService = this;
         nameOfTheService.data = "Some Data";
         nameOfTheService.getSomeData = function(){
               return nameOfTheService.data;
         }     
    }
    

    index.html

    {{serviceExample.data}}

    The main features of Service:

    1. Lazily Instantiated: If the service is not injected it won't be instantiated ever. So to use it you will have to inject it to a module.

    2. Singleton: If it is injected to multiple modules, all will have access to only one particular instance. That is why, it is very convenient to share data across different controllers.

    FACTORY

    Now let's talk about the Factory in AngularJS

    First let's have a look at the syntax:

    app.js:

    var app = angular.module('FactoryExample',[]);
    var factoryController = app.controller('FactoryController', FactoryController);
    var factoryExampleOne = app.factory('NameOfTheFactoryOne', NameOfTheFactoryOne);
    var factoryExampleTwo = app.factory('NameOfTheFactoryTwo', NameOfTheFactoryTwo);
    
    //first implementation where it returns a function
    function NameOfTheFactoryOne(){
       var factory = function(){
          return new SomeService();
        }
       return factory;
    }
    
    //second implementation where an object literal would be returned
    function NameOfTheFactoryTwo(){
       var factory = {
          getSomeService : function(){
              return new SomeService();
           }
        };
       return factory;
    }
    

    Now using the above two in the controller:

     var factoryOne = NameOfTheFactoryOne() //since it returns a function
     factoryOne.someMethod();
    
     var factoryTwo = NameOfTheFactoryTwo.getSomeService(); //accessing the object
     factoryTwo.someMethod();
    

    Features of Factory:

    1. This types of services follow the factory design pattern. The factory can be thought of as a central place that creates new objects or methods.

    2. This does not only produce singleton, but also customizable services.

    3. The .service() method is a factory that always produces the same type of service, which is a singleton. There is no easy way to configure it's behavior. That .service() method is usually used as a shortcut for something that doesn't require any configuration whatsoever.

提交回复
热议问题