In this 16th day of the AngularJS article series, we are going to learning next key players/concept of AngularJS, understanding the concept of Services in AngularJS. Before moving to key players/concepts of AngularJS, please read my previous articles:
- Introduction to AngularJS – Day 1
- Introduction to AngularJS – Day 2
- Introduction to AngularJS – Day 3
- Introduction to AngularJS – Day 4
- Introduction to AngularJS – Day 5
- Introduction to AngularJS – Day 6
- Introduction to AngularJS – Day 7
- Introduction to AngularJS – Day 8
- Introduction to AngularJS – Day 9
- Introduction to AngularJS – Day 10
- Introduction to AngularJS – Day 11
- Introduction to AngularJS – Day 12
- Introduction to AngularJS – Day 13
- Introduction To AngularJS – Day 14
- Introduction To AngularJS – Day 15
Services
In AngularJS services are lazily instantiated and singletons. Services are nothing but the constructor. AngularJS provides many built in services. For example:
Singletons that perform re-usable task:
- Ajax Calls
- Business rules
- Calculations
- Share data between controllers
- Used to make RESTful calls
- Used to handle custom logic
Syntax:
-
- var app = angular.module("myApp", []);
-
-
- app.service("mathService", function () {
-
- this.ADD = function (a, b) {
- return a + b;
- };
-
- this.SUB = function (a, b) {
- return a - b;
- };
-
- this.MULT = function (a, b) {
- return a * b;
- };
-
- this.DIV = function (a, b) {
- return a / b;
- };
- });
The above syntax shows how to create a service in AngularJS. The code shows how to inject service in controller:
-
- app.controller("myController", function ($scope, mathService) {
-
- $scope.add = function () {
- $scope.add_result = mathService.ADD($scope.num1, $scope.num2);
- };
-
- $scope.sub = function () {
- $scope.add_result = mathService.SUB($scope.num1, $scope.num2);
- };
-
- $scope.mult = function () {
- $scope.add_result = mathService.MULT($scope.num1, $scope.num2);
- };
-
- $scope.div = function () {
- $scope.add_result = mathService.DIV($scope.num1, $scope.num2);
- };
-
- });
Example:
In the following example we created a module, service and controller to inject the service in controller and a view to display.
app.js -
- var app = angular.module("myApp", []);
-
-
- app.service("mathService", function () {
-
- this.ADD = function (a, b) {
- return parseInt(a) + parseInt(b);
- };
-
- this.SUB = function (a, b) {
- return a - b;
- };
-
- this.MULT = function (a, b) {
- return a * b;
- };
-
- this.DIV = function (a, b) {
- return a / b;
- };
- });
-
-
- app.controller("myController", function ($scope, mathService) {
-
- $scope.add = function () {
- $scope.add_result = "Addition of two number is " + mathService.ADD($scope.num1, $scope.num2);
- };
-
- $scope.sub = function () {
- $scope.add_result = "Subtraction of two number is " + mathService.SUB($scope.num1, $scope.num2);
- };
-
- $scope.mult = function () {
- $scope.add_result = "Multiplication of two number is " + mathService.MULT($scope.num1, $scope.num2);
- };
-
- $scope.div = function () {
- $scope.add_result = "Division of two number is " + mathService.DIV($scope.num1, $scope.num2);
- };
-
- });
index.html - <!DOCTYPE html>
- <html ng-app="myApp">
- <head>
- <title>Services in AngularJS</title>
- <meta charset="utf-8" />
- <script src="app/angular.min.js"></script>
- <script src="app/app.js"></script>
- </head>
- <body ng-controller="myController">
- <h2>Services in AngularJS(MathService Example)</h2>
- Enter First Number : <input type="text" ng-model="num1" /><br /><br />
- Enter Second Number : <input type="text" ng-model="num2" /><br /><br />
- <input type="button" value="+" ng-click="add()" />
- <input type="button" value="-" ng-click="sub()" />
- <input type="button" value="*" ng-click="mult()" />
- <input type="button" value="/" ng-click="div()" /> <br /><br />
- <h3>{{add_result}}</h3>
- </body>
- </html>
Output:
Now, enter value in text box and click on button you want like ‘+, -, *, /’ and you get the result as follows:
Great, Services in AngularJS with example created successfully!
Summary
I hope that beginners as well as students understand the concept of Services in AngularJS with Example. If you have any suggestions regarding this article, then please contact me. Stay tuned for other concepts of AngularJS coming soon!
Thanks for reading. Learn it! Share it!
Read more articles on AngularJS: