Services: A special unit of code which can be used across different controller.
As there are two types of service available in angularjs:
1.Build-in-Service.
2. Custom service.
However, in custom service there are basically divided into three types:
1. Factory
2.Service(should not be confused with name controller service)
3.Provide(we can say have both factory as well as service).
The General Syntax of above three services is given below for your reference:
*Factory:
var MyApp = angular.module("App", []);
Myapp.controller('emp', function($scope, TaxFactory) {
/*calling service*/
TaxFactory.cal();
}])
// Services getting registered using Module Factory.
app.factory('TaxFactory', function() {
// creating empty serviceobject
var calService = {};
// object with some bussiness logic
calService.cal = function() {
......
}
// returning object that can be used by the controller.
return calService;
}]);
*Service:
var MyApp = angular.module("MyApp", []);
Myapp.controller('MyController', function($scope, TaxService) {
/*calling service*/
TaxService.cal();
}])
// Services getting registered using Module Factory.
Myapp.service('TaxService', function() {
this.cal = function() {
......
};
..........
}]);
*Provider
var MyApp = angular.module("MyApp", []);
Myapp.controller('MyController', function($scope, TaxService) {
TaxService.cal();
});
MyApp.provider('TaxService', function() {
this.cal = function() {
......
};
..........
return Taxservice
}]);
Myapp.config(["TaxServiceProvider", function(TaxServiceProvider) {
TaxServiceProvider.config();
}]); |