Availability of internet connectivity is responsible for the sudden blast in demand for web application development. Web applications have become an important tool for business as it is used for communication with customers, secure data storage, collaboration with employees and providing information to the management. These web apps are developed with the help of frameworks. In this article, we will discuss AngularJS, one of the best frameworks and what is the usage of service function and factory function in AngularJS. 

Want To Get AngularJS Training From Experts? Visit Here AngularJS Training.

What is AngularJS?

 AngularJS is a JavaScript framework that is used to create dynamic web applications. It is one of the most used and popular frameworks having a bunch of advantageous features. AngularJS provides many inbuilt services that are prefixed with a $ symbol such as $http, $location,  $route, $window, etc. Each service is used for a specific task. For eg. the $route is used to define the routing information, the $http is used to make ajax call to get the server data, and so on. 

There are two ways to create a service −

  • Factory
  • Service

What is a Factory in AngularJS?

The factory is a function in AngularJS which is used to return the values whenever the controller needs it. Once the value is created it can be reused by all the services and controllers. 

What is Service in AngularJS?

Subscribe to our youtube channel to get new updates..!

In AngularJS, services are JavaScript functions that perform specific tasks. These are the individual entities which are maintainable and testable and are called by the controllers and filters on requirement basis.

To make the concept more clear, let us understand it through the programming.

For the service code:

function service(name, constructor) {

return factory(name, ['$injector', function($injector) {

return $injector.instantiate(constructor);



Factory code:

var firstModule = angular.module("firstModule", []);

firstModule.factory("firstFactory", function() {

return "a value";


firstModule.controller("FirstController", function($scope, firstFactory) {



Frequently Asked AngularJS Interview Questions

Factory Vs Service

factory() is a method that takes a name and function that are injected in the same way as in service. The major difference between an AngularJS service and AngularJS factory is that service is a constructor function and factory is not. That is why, in case of a factory, we return an object literal instead of using this.

When we call service() function, factory() is the one which is actually called by passing a function that instantiates an object by the constructor. In simple words, we can say that service calls a predefined factory. 


AngularJS is an open-source MVC framework which has been developed by Google itself. It has become the no. 1 choice of the developers to develop web applications. 

We hope you must have clearly understood the difference between Service() and factory() in AngularJS. For further doubts, leave your comments.