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, 2020 in Web Development by kartik
• 37,520 points
619 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, 2020 by Niroj
• 82,880 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, 2020 in Web Development by Niraj

reshown Jan 16, 2020 by Sirajul 2,045 views
0 votes
0 answers

How can I specify Google map with driving direction in jQuery mobile

I have done a Google Maps based ...READ MORE

Jul 20, 2022 in Web Development by gaurav
• 23,260 points
557 views
0 votes
0 answers

how can i get the url of the content( home.html) in adress bar by jquery load() function?

I am using jquery load() function to ...READ MORE

Jul 28, 2022 in Web Development by gaurav
• 23,260 points
586 views
0 votes
0 answers

How can I use jQuery in Greasemonkey?

I tried putting this line but it ...READ MORE

Jul 28, 2022 in Web Development by gaurav
• 23,260 points
960 views
0 votes
1 answer
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, 2020 in Web Development by anonymous
• 37,520 points
901 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, 2020 in Web Development by Niroj
• 82,880 points

edited Jan 21, 2020 by Niroj 1,099 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, 2020 in Web Development by Niroj
• 82,880 points

edited Jan 21, 2020 by Niroj 882 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, 2020 in Web Development by Niroj
• 82,880 points

edited Jan 21, 2020 by Niroj 2,906 views
+2 votes
1 answer

What are the different ways of using angularjs scope?

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

answered Jan 21, 2020 in Web Development by Niroj
• 82,880 points
669 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP