Confused about Service vs Factory

后端 未结 20 2329
轻奢々
轻奢々 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

    <div ng-controller = "ServiceExampleController as serviceExample">
       {{serviceExample.data}}
    </div>
    

    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.

    0 讨论(0)
  • 2020-11-22 09:43

    All providers work the same way. The different methods service, factory, provider just let you accomplish the same thing in less code.

    P.S. There's also value and constant.

    Each special case down the chain starting with provider and ending with value has an added limitation. So to decide between them you have to ask yourself which let's you accomplish what you want with less code.

    Here is a picture that shows you what I mean:

    You can a breakdown and reference guide on the blog post I got this image from:

    http://www.simplygoodcode.com/2015/11/the-difference-between-service-provider-and-factory-in-angularjs/

    0 讨论(0)
提交回复
热议问题