In AngularJs, like other features, one of the main features is service provider. In Angular Js, service is basically a single tone objects which used to perform for a specific tasks.
Why Need Service
Basically Angular JS services represents the separation of concern using service architectures. Actually AngularJs services consists of JavaScript functions and also used for a particular tasks. May be one service contains multiple JavaScript function for different task. But precisely it is important that each of the function must be perform simple responsibility. Actually in this way we can easily maintain and test the code. We need to call the services in controllers, directives and filters as per our requirement. Services are normally injected using dependency injection mechanism of AngularJs. Actually angular js provides many inbuilt services for our uses, eg. $http, $route, $location etc. Each service is responsible for a specific work as example , $http is used for make ajax request call to get or post the server data. $route is defined for routing information.
How to Create Service
There are two ways of creating services –
- Factory – In this method, we need to define factory first and then assign method to it.
- testapp.factory(‘testservice’, function(){
- return {
- method : function(){
- --------
- --------
- },
- method : function(){
- --------
- --------
- }
- };
- });
- Service – In this method, we need to define service first and then assign method to it.
- testapp.service(‘testservice’, function(){
- this.method1 = function(){
- ……………………………
- }
- this.method1 = function(){
- ……………………………
- }
- });
- 'use strict';
- var testApp = angular.module('TestApp', []);
- testApp.factory("calculate", function () {
- return {
- add: function (args1, args2) {
- return args1 + args2;
- },
- subtract: function (args1, args2) {
- return args1 - args2;
- },
- multiply: function (args1, args2) {
- return args1 * args2;
- },
- divide: function (args1, args2) {
- return args1 / args2;
- },
- };
- });
- testApp.controller('IndexController', ['$http', 'calculate', function ($http, calculate) {
- var self = this;
- self.message = 'Angular Service';
- self.fnAdd = function () {
- self.resultNumber = calculate.add(parseFloat(self.firstNumber), parseFloat(self.secondNumber));
- }
- self.fnSub = function () {
- self.resultNumber = calculate.subtract(parseFloat(self.firstNumber), parseFloat(self.secondNumber));
- }
- self.fnMultiply = function () {
- self.resultNumber = calculate.multiply(parseFloat(self.firstNumber), parseFloat(self.secondNumber));
- }
- self.fnDivide = function () {
- self.resultNumber = calculate.divide(parseFloat(self.firstNumber), parseFloat(self.secondNumber));
- }
- }]);
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>Directive Types</title>
- <link href="../../RefStyle/bootstrap.min.css" rel="stylesheet" />
- <script src="../../Scripts/angular.min.js"></script>
- <script src="SampleService.js"></script>
- <script src="IndexController.js"></script>
- </head>
- <body data-ng-app="TestApp" data-ng-controller="IndexController as model">
- <div class="row animated fadeInRight">
- <div class="col-lg-12">
- <div class="rowDiv">
- <h3>{{model.message}}</h3>
- </div>
- </div>
- <div class="rowDiv">
- <div class="col-lg-3">
- First Number
- </div>
- <div class="col-lg-9">
- <input type="text" data-ng-model="model.firstNumber" />
- </div>
- </div>
- <div class="rowDiv">
- <div class="col-lg-3">
- Second Number
- </div>
- <div class="col-lg-9">
- <input type="text" data-ng-model="model.secondNumber" />
- </div>
- </div>
- <div class="rowDiv">
- <div class="col-lg-3">
- Result
- </div>
- <div class="col-lg-9">
- <input type="text" data-ng-model="model.resultNumber" readonly />
- </div>
- </div>
- <div class="rowDiv">
- <div class="col-lg-3">
- </div>
- <div class="col-lg-9">
- <input type="button" value="+" data-ng-click="model.fnAdd();" />
- <input type="button" value="-" data-ng-click="model.fnSub();" />
- <input type="button" value="x" data-ng-click="model.fnMultiply();" />
- <input type="button" value="/" data-ng-click="model.fnDivide();" />
- </div>
- </div>
- </div>
- </body>
- </html>


Pradeep SahooPosted Mar 18, 2016, 9:57 AM
Good Information . Thanks for sharing & keep writing..