I have 4+ experience and working as software Engineer with Hytech professionals India PVT ltd and also working as Consultant,Trainer,Blogger and Author on Microsoft technologies,Also I have Trained more than 500+ IT professionals.I have knowledge about Asp.net,C#,SQL,Project management,replication,MVC,WebApi,SSRS,PostMan,TFS,SVN,Swagger,WMI,Agile,SignalR,Angularjs,IIS configuration.

Part 11::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 Part 11::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, 4 February 2016

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

Post a Comment

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