11+ Year IT Industry Experience, Working as Technical Lead with Capgemini | Consultant | Leadership and Corporate Trainer | Motivational and Technical Speaker | Career Coach | Author | MVP | Founder Of RVS Group | Trained more than 4000+ IT professionals | Azure | DevOps | ASP.NET | C# | MVC | WEB API | ANGULAR | TYPESCRIPT | MEAN | SQL | SSRS | WEB SERVICE | WCF... https://bikeshsrivastava.blogspot.in/ http://bikeshsrivastava.com/

What's the difference between factory,service and provider?

Factory:-
When you're utilizing a Factory you make an item, add properties to it, then give back that same article. When you pass this administration into your controller, those properties on the item will now be accessible in that controller through your plant.
factory1-4
An industrial facility is a basic capacity which permits you to include some rationale before making the item. It gives back the made item. 
It is only a gathering of capacities such as a class. Subsequently, it can be instantiated in various controllers when you are utilizing it with constructor capacity.

Service:-
When you're utilizing Service, it's instantiated with the "new" watchword. Therefore, you'll add properties to "this" and the administration will give back 'this'. When you pass the administration into your controller, those properties on "this" will now be accessible on that controller through your administration.
ServiceExample2
An administration is a constructor capacity which makes the article utilizing new watchword. You can add properties and capacities to an administration object by utilizing this catchphrase. Not at all like production line, it doesn't return anything. 
It is a singleton object. Use it when you have to share a solitary article over the application. For instance, confirmed client points of interest.

Provider:-It returns  value always  by using $get() function.
Providers are the only service you can pass into your .config() function with provider name. Use a provider when you want to provide module-wide configuration for your service object before making it available.
p16

The greatest thing to recall about Providers is that they're the main administration that you can go into the app.config part of your application. This is of tremendous significance in case you're expecting to adjust some part of your administration object before it's accessible all over the place else in your application. Albeit fundamentally the same to Services/Factories, there are a couple of contrasts which we'll talk about.
Initially we set up our Provider similarly we did with our Service and Factory. The variables underneath are our "private" and aide capacity.

Example of Factory,Service and Provider in AngularJs:-
<html>
<head>
<title>AngularJS Service Factory and Providers</title>
<script src="angular.js"></script>
</head>
<body>
<div class="container" style="padding-top:20px;">
<div ng-app="myApp" ng-controller="myController">
<p>From Service: {{serviceName}}</p>
<p>From Factory: {{factoryName}}</p>
<p>From Provider: {{providerName}}</p>
</div>
</div>
<script>
//defining module
var app = angular.module('myApp', []);
//defining service
app.service('myService', function () {
this.name = '';
this.setName = function (newName) {
this.name = newName;
return this.name;
};
});
//defining factory
app.factory('myFactory', function () {
var serviceObj = {};
serviceObj.name = '';
serviceObj.setName = function (newName) {
serviceObj.name = newName;
};
return serviceObj;
});
//defining provider
app.provider('configurable', function () {
var privateName = '';
this.setName = function (newName) {
privateName = newName;
};
this.$get = function () {
return {
name: privateName
};
};
});
//configuring provider
app.config(function (configurableProvider) {
configurableProvider.setName("Bikesh Srivastava----Provider Example");
});
//defining controller
app.controller('myController', function ($scope, myService, myFactory, configurable) {
$scope.serviceName = myService.setName("Bikesh srivastava---Service Example");
myFactory.setName("Bikesh srivastava -----Factory Example");
$scope.factoryName = myFactory.name;
$scope.providerName = configurable.name;
});
</script>
</body>
</html>
Note:- Factories are functions that return the object always , while services are constructor functions of the object which are instantiated with the "new" keyword.
You have just read an article that categorized by title AngularJs by title What's the difference between factory,service and provider?. You can bookmark this page with a URL https://bikeshsrivastava.blogspot.com/2016/02/part-11whats-difference-between.html. Thank You!
Author: Bikesh Srivastava - Thursday, February 4, 2016

There are currently no comments for "What's the difference between factory,service and provider?"

Post a Comment

Life Is Complicated, But Now programmer Can Keep It Simple.