Angular Service and Factory Tutorial

Angular Service and Factory Tutorial

Today, We want to share with you Angular Service and Factory Tutorial.
In this post we will show you Angular Service and Factory Tutorial, hear for Angular Service and Factory Tutorial we will give you demo and example for implement.
In this post, we will learn about Angular Service and Factory Tutorial with an example.

AngularJS Service and Factory

AngularJS provides many more useful inbuilt services for example Like, $https:, $route, $window and $location etc.

All angularjs internal services starts with $ sign.)

AngularJS most supports the concepts of “Separation of Concerns” slogan using services architecture.

READ :  Vuejs Introduction Advanced Features Hello World Example

There are two most useful ways to create a service.

  • factory
  • service

AngularJS internal services

[php]
module.controller(‘myfirstController’, function($http){
//…something code
});

module.controller(‘mysecondController’, function($window){
//……something code
});
[/php]

AngularJS custom services : example

[php]
var module = angular.module(‘myapp’, []);
module.service(‘userService’, function(){
this.users = [‘Ravi’, ‘Jignesh’, ‘Harshad’];
});
[/php]

AngularJS factory Method : Example

[php]
module.factory(‘userService’, function(){
var fac = {};
fac.users = [‘Ravi’, ‘Jignesh’, ‘Harshad’];
return fac;
});
[/php]

AngularJS Service vs Factory : Example

[php]
module.service( ‘serviceName’, function );

module.factory( ‘factoryName’, function );
[/php]

what is factory?

AngularJS factory is a simple Javascript function(js function) which allows you to add some (logic data and return)logic before creating the object(recreated object). and It returns the created new object.

READ :  Windows 10 Product Keys 100% Working Activation - windows 10 activation key free

AngularJS Factory Syntax with Example

Factory Syntax

[php]
var module = angular.module(‘infinityknowApp’, []);
module.factory(‘yourserviceName’, function(){
return serviceObject; //return something…
});
[/php]

Factory Example

[php]


My first AngularJS Factory and Service code
https://infinityknow.com/lib/angular.js

//Simple Defining Factory
var infinityknowApp = angular.module(‘app’, []);

infinityknowApp.factory(‘calculatorService’, function(){
var calculator = {};
calculator.multiply = function(a, b) { return a * b };
calculator.add = function(a, b) { return a + b };
calculator.substract = function(a, b) { return a – b };
calculator.divide = function(a, b) { return a / b };
return calculator;
});

infinityknowApp.controller(‘CalculatorController’, function($scope, calculatorService) {
$scope.do_simple_mul = function() {
$scope.answer = calculatorService.multiply($scope.number,$scope.number);
}
$scope.do_simple_add = function() {
$scope.answer = calculatorService.add($scope.number,$scope.number);
}
$scope.do_simple_sub = function() {
$scope.answer = calculatorService.substract($scope.number,$scope.number);
}
$scope.do_simple_div = function() {
$scope.answer = calculatorService.divide($scope.number,$scope.number);
}
});

AngulerJS Factory Example
Enter your Valid Number:


Enter your Valid Number:

READ :  Angular Expand and Collapse Example


Enter your Valid Number:


Enter your Valid Number:

Your Calculator Answer : {{answer}}


Solution of AngularJs All Problems For infinityknow.com

My First Web-Application in angularjs, So I am very happy and 1000+ more then people are used of infinityknow.com

[/php]