Difference between Factory and Service in AngularJS

Difference between Factory and Service in AngularJS

The Simple Meaning of services are reusable (many times call services) singleton objects.

Difference between Factory and Service in AngularJS

Service vs Factory : What is the difference between a factory and service in AngularJS?

The difference between factory in angular and service in angular is just like the difference between a simple function and an object.




AngularJS Factory Provider : features

  • angular,Gives us the function’s to return value example. You just create simple an object, add to other properties to it, now then return that same any object.When you simple pass It service into your simple controller and get data, those all properties on the get object will new now be available in that same controller through your latest factory. (Like as a Hypothetical Scenario.)
  • simple Singleton and will only be created once times.
  • (More times..)Reusable components used.
  • (linking data)Factory are a great or best way for communicating between your controllers like all sharing data.
  • then Can use other more dependencies.
  • simple Usually used when the service instance requires simple or complex creation logic maintians.
  • and then Cannot be injected in .config() simple function.
  • It’s Used for non configurable(without config) services.
  • If you are using an any object, so you could use best way to the factory provider.
  • simple Syntax: module.factory(‘yourfactoryname’, function);
Also Read :   ngShow and ngHide in angularjs with Example

AngularJS Service Provider : features

  • 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.
  • The Services are used for one or more communication between controllers to sharing data in communicating.
  • Now You can add simple properties and more functions to a service object by using the this(this is a keyword) keyword.
  • (depedeble)Dependencies are injected any as constructor more arguments
  • so we Used for simple creation complex logic
  • same like as a factory to Cannot be injected any in .config() function.
  • If you are simple using a any class you could use html the service provider.
  • simple Syntax: module.service(‘yourserviceName’, function);
Also Read :   Advanced Pagination with PHP, MySQL and jQuery - Custom Pagination

AngularJS Common features

  • Simple Singleton and will only be created once.
  • both of Reusable component.
  • Used for communication between your controllers to share data every place.
  • and last one is Cannot be injected in “.config()” function.

AngularJS .service with syntax and Example

module.service('yourServicename', function() {

    this.method1 = function() {
			//..somecode...
            //..method1 logic
        }

    this.method2 = function() {
			//..somecode...
            //..method2 logic
        }
});

AngularJS .factory with syntax and Example

module.factory('yourfactoryname', function() {

    var factory = {}; 

    factory.method1 = function() {
			//..somecode...
            //..method1 logic
			 //..somecode...
        }

    factory.method2 = function() {
			//..somecode...
            //..method2 logic
        }

    return factory;
});




View Demo

   

Add a Comment

Your email address will not be published.