Confused about Service vs Factory

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

    The basic difference, is that provider allows to set primitive (non-objects), array, or callback function values into the factory declared variable, and thus if returning an object it has to be explicitly declared and returned.

    On the other hand a service can only be used to set the service declared variable to an object, thus we can avoid the explicit creation and returning of the objects, while on the other hand it allows usage of the this keyword.

    Or in short words "provider is a more generic form while service is limited to objects only".

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

    You can understand the difference with this analogy - Consider the difference between a normal function that will return some value and constructor function that will get instantiated using new keyword.So creating factory is just similar to create normal function that will return some value(primitive or an object) whereas creating service is like creating constructor function(OO class) of which we can create instance using new keyword. The only thing to notice is here is that when we use Service method to create services it will automatically create instance of it using dependency injection mechanism supported by AngularJS

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

    For me the revelation came when I realise that they all work the same way: by running something once, storing the value they get, and then cough up that same stored value when referenced through Dependency Injection.

    Say we have:

    app.factory('a', fn);
    app.service('b', fn);
    app.provider('c', fn);
    

    The difference between the three is that:

    1. a's stored value comes from running fn , in other words: fn()
    2. b’s stored value comes from newing fn, in other words: new fn()
    3. c’s stored value comes from first getting an instance by newing fn, and then running a $get method of the instance

    which means, there’s something like a cache object inside angular, whose value of each injection is only assigned once, when they've been injected the first time, and where:

    cache.a = fn()
    cache.b = new fn()
    cache.c = (new fn()).$get()
    

    This is why we use this in services, and define a this.$get in providers.

    Hope this helps.

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

    Here are the primary differences:

    Services

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

    Result: When declaring serviceName as an injectable argument you will be provided with the instance of a function passed to module.service.

    Usage: Could be useful for sharing utility functions that are useful to invoke by simply appending () to the injected function reference. Could also be run with injectedArg.call( this ) or similar.

    Factories

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

    Result: When declaring factoryName as an injectable argument you will be provided the value that is returned by invoking the function reference passed to module.factory.

    Usage: Could be useful for returning a 'class' function that can then be new'ed to create instances.

    Also check AngularJS documentation and similar question on stackoverflow confused about service vs factory.

    Here is example using services and factory. Read more about AngularJS service vs factory.

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

    “Factory” and “Service” are different ways of doing DI (Dependency injection) in angular.

    So when we define DI using “service” as shown in the code below. This creates a new GLOBAL instance of the “Logger” object and injects it in to the function.

    app.service("Logger", Logger); // Injects a global object
    

    When you define DI using a “factory” it does not create a instance. It just passes the method and later the consumer internally has to make calls to the factory for object instances.

    app.factory("Customerfactory", CreateCustomer);
    

    Below is a simple image which shows visually how DI process for “Service” is different than “Factory”.

    enter image description here

    Factory should be used When we want to create different types of objects depending on scenarios. For example depending on scenario we want to create a simple “Customer” object , or “Customer” with “Address” object or “Customer” with “Phone” object. Here is a detailed explanation of this paragraph

    Service should be used When we have utility or shared functions to be injected like Utility , Logger , Error handler etc.

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

    For short and simple explanation refer https://stackoverflow.com/a/26924234/5811973.

    For detailed explanation refer https://stackoverflow.com/a/15666049/5811973.

    Also from angularJs documentation:

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