Home  >  Blog  >   AngularJS

Understanding AngularJS: Factory vs Service

Rating: 4

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. 

If you want to become a Angular Certified Specialist, then visit Mindmajix - A Global online training platform: “Angular Online Certification Training”.  This course will help you to achieve excellence in this domain.

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 an ajax calls 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 that 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. 

 MindMajix YouTube Channel

What is Service in AngularJS?

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

To make the concept more clear, let us understand it through 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 an AngularJS factory is that a service is a constructor function and a factory is not. That is why, in the case of a factory, we return an object literal instead of using this.

When we call service() function, factory() is the one that 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 that 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.


Join our newsletter

Stay updated with our newsletter, packed with Tutorials, Interview Questions, How-to's, Tips & Tricks, Latest Trends & Updates, and more ➤ Straight to your inbox!

Course Schedule
Angular TrainingApr 23 to May 08View Details
Angular TrainingApr 27 to May 12View Details
Angular TrainingApr 30 to May 15View Details
Angular TrainingMay 04 to May 19View Details
Last updated: 03 Apr 2023
About Author

Ravindra Savaram is a Technical Lead at Mindmajix.com. His passion lies in writing articles on the most popular IT platforms including Machine learning, DevOps, Data Science, Artificial Intelligence, RPA, Deep Learning, and so on. You can stay up to date on all these technologies by following him on LinkedIn and Twitter.

read more