AngularJS Services

In AngularJS, services are singletons objects that are instantiated only once per app. Services are used to organize and share code across your app.

It means, services can play major role to perform only specific tasks. Using $scope you can create model data and bind to the views, it does not responsible to manipulate data. Service object contains few useful functions that you can call from Controllers, Directive and Filters etc.

AngularJS comes with several built-in services such as $http, $route, $window, $location etc. AngularJS also allows you to create your own service by registering the service. Once a service is registered, the AngularJS compiler can reference it and load it as a dependency for runtime use.

AngularJS provide two ways to create a service:

Syntax1: Service

var module = angular.module('myapp', []);
module.service('customerService', function(){
    this.customers = ['Jimi', 'Tom', 'Jake'];
});

Syntax2: Factory

var module = angular.module('myapp', []);
module.factory('customerService', function(){
    var cust = {};
    cust.customers = ['Jimi', 'Tom', 'Jake'];
    return cust;
});

Example

<html>
<head>
<title>My first AngularJS Service code</title>
<Script SRC="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js">
</Script>
<Script>
//Defining Services Using Service
var app = angular.module('app', []);
 
app.service('MathService', function() {
    this.add = function(a, b) { return a + b };   
    this.subtract = function(a, b) { return a - b }; 
    this.multiply = function(a, b) { return a * b };
    this.divide = function(a, b) { return a / b };
});
 
app.service('CalculatorService', function(MathService){
    this.square = function(a) { return MathService.multiply(a,a); };
    this.cube = function(a) { 
		return MathService.multiply(a, MathService.multiply(a,a)); 
		};
});
 
app.controller('CalculatorController', function($scope, CalculatorService) {
    $scope.doSquare = function() {
        $scope.answer = CalculatorService.square($scope.number);
    }
    $scope.doCube = function() {
        $scope.answer = CalculatorService.cube($scope.number);
    }
});
</Script>
</head>
<body>

<div ng-app="app">
    <div ng-controller="CalculatorController">
        Enter a number:
        <input ng-model="number" type="number">
        <button ng-click="doSquare()">X<sup>2</sup></button>
        <button ng-click="doCube()">X<sup>3</sup></button>
         
        <div>Answer: {{answer}}</div>
    </div>
</div>

</div>
</body>
</html>
Try Now

See Also

AngularJS Built-in Filters
AngularJS Built-in Directives
AngularJS Expression
AngularJS Built-in Events
AngularJS Scope Methods
AngularJS Built-in Services
AngularJS Controller Inheritance
AngularJS Custom Directives


 
 
SHARE THIS IF YOU LIKE!
submit to reddit

SUBSCRIBE FOR LATEST UPDATES
HAVE WE CONNECTED ON FACEBOOK?
TOP TUTORIALSTOP CODE EXAMPLESTOP LIVE DEMOLINKSSTAY CONNECTED
HTML Tutorial
HTML5 Tutorial
Bootstrap3 Tutorial
Javascript Tutorial
TypeScript Tutorial
AngularJS Tutorial
CSharp Tutorial
PHP Tutorial
Developer Tools
Javascript
AngularJS
Google MAP API V3
ASP.NET
Javascript
AngularJS
Google MAP API V3
Contact Us
Advertise with Us
Privacy Policy
Disclaimer
Stay connected. We actually like you. If you want to get up to the minute updates, then connect with us on your fav below.
 
Twitter   Linkedin   Facebook   Google+    RSS   Pinterest
 
Protected by Copyscape Original Content Checker
 
 
Copyright ©2017 www.techstrikers.com Unauthorized reproduction/replication of any part of this site is prohibited.