« Previous | 1 | 2 | 3 | Next »
AngularJS supports the concepts of "Separation of Concerns" using services architecture.

Services are javascript functions and are responsible to do a specific tasks only.

This makes them an individual entity which is maintainable and testable.

Controllers, filters can call them as on requirement basis. Services are normally injected using dependency injection mechanism of AngularJS.

AngularJS provides many inbuilt services for example, $http, $route, $window, $location etc.
Each service is responsible for a specific task for example, $http is used to make ajax call to get the server data.

$route is used to define the routing information and so on. Inbuilt services are always prefixed with $ symbol.

There are two ways to create a service.
factory
service
Using factory method
Using factory method, we first define a factory and then assign method to it.
var mainApp = angular.module("mainApp", []);
mainApp.factory('MathService', function() {
var factory = {};
factory.multiply = function(a, b) {
return a * b
}
return factory;
});
Using service method
Using service method, we define a service and then assign method to it. We've also injected an already available service to it.
mainApp.service('CalcService', function(MathService){
this.square = function(a) {
return MathService.multiply(a,a);
}
});
« Previous | 1 | 2 | 3 | Next »


copyright © 2014 - all rights riserved by javatechnologycenter.com