AngularJS Service: is used for sharing utility functions with the service reference in the controller. Service is singleton in nature so for once service only one instance is created in the browser and the same reference is used throughout the page.
- Service syntax:module.service( 'serviceName', function );
AngularJS Factory: the purpose of Factory is also same as Service however in this case we create a new object and add functions as properties of this object and at the end we return this object.
- Factories:module.factory( 'factoryName', function );
Example- <div ng-app="Myapp">
- <div ng-controller="exampleCtrl">
- <input type="text" ng-model="num.firstnumber" />
- <input type="text" ng-model="num.secondnumber" />
- <input type="button" ng-click="Factoryclick()" value="Factoryclick" />
- <input type="button" ng-click="servclick()" value="Serviceclick" /> factory result {{facresult}} service result {{secresult}} </div>
- </div>
- var myapp = angular.module('Myapp', []);
- myapp.controller('exampleCtrl', ['$scope', '$http', 'factories', 'services', function (scope, http, fac, ser)
- {
- scope.Factoryclick = function ()
- {
- var firstnumber = parseInt(scope.num.firstnumber);
- var secondnumber = parseInt(scope.num.secondnumber);
- scope.facresult = fac.sumofnums(firstnumber, secondnumber);
- }
- scope.servclick = function ()
- {
- var firstnumber = parseInt(scope.num.firstnumber);
- var secondnumber = parseInt(scope.num.secondnumber);
- debugger;
- scope.secresult = ser.sersumofnums(firstnumber, secondnumber);
- }
- }]);
- myapp.factory('factories', function ($http)
- {
- return {
- sumofnums: function (a, b)
- {
- return a + b;
- }
- }
- });
- myapp.service('services', function ($http)
- {
- debugger;
- this.sersumofnums = function (a, b)
- {
- return a + b;
- };
- });