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

angularCustoms 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',[]);

 

app.provider('myProvider',function(){

 

                this.$get = function(){

 

                                return "my first provider";

 

                }

 

})
Inject provider In your controller and use them.
app.controller('myController',function(myProvider){

 

    console.log('my provider value',myProvider);

 

})
factory-
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',[]);

 

app.factory('myFactory',function(){

 

                                 return  new function(){

 

                                                          this.getValue = function(){

 

                                                                     return "my first factory";

 

                                                     }

 

                 }

 

            }})

 

app.controller('myController',function(myFactory){

 

   console.log('my factory value',myFactory.getValue());

 

})
Service-
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',[]);

 

app.factory('myService',function(){

 

                                                this.getValue = function(){

 

                                                     return "my Object value

 

                                    }

 

}

 

})

 

app.controller('myController',function(myService){

 

  console.log('my Service value',myService.getValue());

 

})
Value-
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',[]);

 

app.value('myValue',{

 

                name:'myName',

 

                city:'jaipur',

 

                age: 18

 

}

 

})

 

 

 

app.controller('myController',function(myValue){

 

         console.log('my value name',myValue.name);

 

         console.log('my value city',myValue.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.

Leave a Reply

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