In how many ways we can develop custom service make available in AngularJS?

0 votes
In an MVC model of angularjs there is a terms know as controller. So i wanted to know how these controller interact to make service available as we wanted them to as a server do.
Jan 29 in Web Development by kartik
• 10,760 points
25 views

1 answer to this question.

0 votes

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();
}]);
answered Jan 29 by Niroj
• 21,680 points

Related Questions In Web Development

0 votes
1 answer

How many methods of including css in an HTML document?

Hello fignithi, yes!!,you can use external file for css ...READ MORE

answered Jan 16 in Web Development by Niraj

reshown Jan 16 by Sirajul 44 views
+1 vote
1 answer

What are the different types of pop-up boxes available in JavaScript?

There are  basically three types of pop-up ...READ MORE

answered Jan 22 in Web Development by Niroj
• 21,680 points
53 views
0 votes
1 answer

What is data binding in AngularJS?

Data binding is synchronization of data between the ...READ MORE

answered Jan 23 in Web Development by Niroj
• 21,680 points
39 views
0 votes
0 answers

Anyone can help me out to understand the semantic of (document.getElementBYId("demo").innerHTML="Hello") ?

Hello guys, Can Someone helps me to find ...READ MORE

Jan 17 in Web Development by anonymous
• 10,760 points
47 views
+1 vote
1 answer

What is the relationship between angularjs Scope with controller/view?

Let us consider the below block: <div ng-controller="emp"> ...READ MORE

answered Jan 20 in Web Development by Niroj
• 21,680 points

edited Jan 21 by Niroj 67 views
+1 vote
1 answer

What are pseudo class in css??

Hey, The state of an element is controlled  by ...READ MORE

answered Jan 20 in Web Development by Niroj
• 21,680 points

edited Jan 21 by Niroj 49 views
+1 vote
1 answer

How to access the Angularjs scope of a particular html element from our console?

Hello, You should follow the below steps:-- 1.Compile and ...READ MORE

answered Jan 21 in Web Development by Niroj
• 21,680 points

edited Jan 21 by Niroj 52 views
+1 vote
1 answer

What are the different ways of using angularjs scope?

Hey!! basically there the three ways of using ...READ MORE

answered Jan 21 in Web Development by Niroj
• 21,680 points
37 views