How to use custom services in angular (Provider,Service,Factory,Value,Constant)

Customs services in angular mean you can create your own services or you can create your own unit of code which can be used in all module or controller. Custom services mean you write your own reusable code.

Types of custom services

  • 1 – provider
  • 2 – service
  • 3- factory 
  • 4- value
  • 5- constant
Provider –

provider basically the head of services and factory. Means internally all services are used the provider. When we use providers means we have little more control than other services.

The provider only executes one time when controller injects provider it only calls first time and after then if any other controller calls them then it’s return first time loaded value.

Ex- create a provider which return simple string

var app = angular.module('myApp',[]);
  this.$get = function(){
  return "my first provider";
Inject provider In your controller and use them.
   console.log('my provider value',myProvider);

the factory method is a simple function that returns objects. when you use factory method it means you are creating a reusable object which is called by injecting factory in any controller.

var app = angular.module('myApp',[]);
   return  new function(){
      this.getValue = function(){
         return "my first factory";
   console.log('my factory value',myFactory.getValue());

in angular service is work same as a factory method. Service hold a reference to any object. You have to inject service and can use it in all project. Service use this keyword for

holding the reference of any object.

var app = angular.module('myApp',[]);
      this.getValue = function(){
         return "my Object value
   console.log('my Service value',myService.getValue());

value service use when we want to store temporary data means that data can be modified in any controller it can be store string, object, array,  number, then we use value service.

Ex. In example, we are using  object

var app = angular.module('myApp',[]);
      age: 18
   console.log('my value name',;
   console.log('my value city',;
   console.log('my value age',myValue.age);
Constant –

constant service is much same as value. Constant can inject anywhere including 

.config method but value not injected.

Constant cannot store object but a value can.

One thought to “How to use custom services in angular (Provider,Service,Factory,Value,Constant)”

Leave a Reply

Your email address will not be published. Required fields are marked *