Sharing Top Content from the Angular-sphere.

Understanding AngularJS Factory, Service and Provider

  • Keywords : factory vs service vs provider, difference between factory and service and provider, when to use factory service or provider

    In AngularJS, services are reusable singleton objects that are used to organize and share code across your app.

  • A factory is a simple function which allows you to add some logic before creating the object.
  • A service is a constructor function which creates the object using new keyword.
  • You can add properties and functions to a service object by using this keyword.
  • I hope you will enjoy the AngularJS factory, service and provider functions while developing your app with AngularJS.

In AngularJS, services are reusable singleton objects that are used to organize and share code across your app. They can be injected into controllers, filters, directives. AngularJS provides you three ways : service, factory and provider to create a service.

@TarekAHF: Understanding AngularJS Factory, Service and Provider via @twitterapi

In AngularJS, services are reusable singleton objects that are used to organize and share code across your app. They can be injected into controllers, filters, directives. AngularJS provides you three ways : service, factory and provider to create a service.

A factory is a simple function which allows you to add some logic before creating the object. It returns the created object.

app.factory(‘serviceName’,function(){ return serviceObj;})

It is just a collection of functions like a class. Hence, it can be instantiated in different controllers when you are using it with constructor function.

A service is a constructor function which creates the object using new keyword. You can add properties and functions to a service object by using this keyword. Unlike factory, it doesn’t return anything.

app.service(‘serviceName’,function(){})

It is a singleton object. Use it when you need to share a single object across the application. For example, authenticated user details.

A provider is used to create a configurable service object. It returns value by using $get() function.

//creating a service app.provider(‘serviceName’,function(){}); //configuring the service app.config(function(serviceNameProvider){});

When you need to provide module-wise configuration for your service object before making it available.

AngularJS Service Factory and Providers

From Service: {{serviceName}}

From Factory: {{factoryName}}

From Provider: {{providerName}}

I hope you will enjoy the AngularJS factory, service and provider functions while developing your app with AngularJS. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Understanding AngularJS Factory, Service and Provider

Comments are closed, but trackbacks and pingbacks are open.