AngularJS Factory Method Last Updated : 19 Oct, 2023 Comments Improve Suggest changes Like Article Like Report AngularJS Factory Method makes the development process of AngularJS applications more robust. A factory is a simple function that allows us to add some logic to a created object and return the created object. The factory is also used to create/return a function in the form of reusable code which can be used anywhere within the application. Whenever we create an object using a factory it always returns a new instance for that object. The object returned by the factory can be integrated(injectible) with different components of the Angularjs framework such as controller, service, filter, or directive. Use: A practical Scenario Factory generally acts as a container or class for a collection of functions that fulfills different features of the application. When used with a constructor function it can be initiated within different Controllers. Syntax: module.factory( 'factoryName', function(){ Custom code....});Example 1: The following example illustrates the use of factory code instantiated inside a controller to generate a random number. HTML <!DOCTYPE html> <html> <head> <title>Factory Example 1</title> <script src= "https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"> </script> <script> var application = angular.module('myApp', []); application.factory('random', function () { var randomObject = {}; var number = Math.floor(Math.random() * 100); randomObject.generate = function () { return number; }; return randomObject; }); application.controller('thisapp', function ($scope, random) { $scope.generateRandom = function () { $scope.randomNumber = random.generate(); }; }); </script> </head> <body> <h1 style="color:green">GeeksforGeeks</h1> <h2>Factory Examples</h2> <div ng-app="myApp" ng-controller="thisapp"> <button ng-click="generateRandom()"> Generate Random Number </button> <br>{{randomNumber}} </div> </body> </html> Output: On Clicking the generate random number button we get a different number every time. In this example, we use the factory method to define a function that carries a variable and using the Math.random we store a random value to that variable every time this function is called. This function is then called in the controller whose $scope variable carries the random value from the called function we then call this controller to our HTML code to display the result. Example 2: This example makes use of a factory to create a function to find the addition or subtraction of two numbers. this function is then loaded in the controller $scope variable which passes them to the HTML code for displaying the results. HTML <!DOCTYPE html> <html> <head> <title>Factory Example 2</title> <script src= "https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"> </script> <script> var application = angular.module('myApp', []); application.factory('MyFactoryService', function () { var factory = {}; factory.Subtract = function (a, b) { return a - b; }; factory.Add = function (a, b) { return a + b; }; return factory; }); application.controller('thisapp', function ( $scope, MyFactoryService) { $scope.result = function () { $scope.results = MyFactoryService.Subtract($scope.num1, $scope.num2) }; $scope.result2 = function () { $scope.results = MyFactoryService.Add($scope.num1, $scope.num2) }; }); </script> </head> <body> <h1 style="color:green">GeeksforGeeks</h1> <h2>Factory Example 2</h2> <div ng-app="myApp" ng-controller="thisapp"> <p> Enter A Number: <input type="number" ng-model="num1" /> <br /> Enter A Number: <input type="number" ng-model="num2" /> <br /> </p> <button ng-click="result()">Subtract</button> <button ng-click="result2()">Add</button> <p>Results: {{results}} </div> </body> </html> Output: Comment More infoAdvertise with us Next Article AngularJS Factory Method R RohaanKhan Follow Improve Article Tags : Web Technologies AngularJS Similar Reads AngularJS $cacheFactory Service The $cacheFactory service in AngularJS is a factory function that creates new instances of the Cache object, which is a simple in-memory cache that stores key-value pairs. The Cache object is useful for storing data that is expensive to retrieve, such as data that comes from a server or data that is 5 min read AngularJS $controller Service AngularJS applications depend on a controller to control the flow of data through an AngularJS application. AngularJS architecture uses the MVC model i.e the Model View Controller. The model is responsible for maintaining the application data, the View for displaying data or some part of data to the 5 min read AngularJS Controllers In this article, we will see the Controller in AngularJS along with knowing how Controller works, the concept of the Controller method & how the Controller can be implemented in an external. We will understand all these aspects with the help of their implementation & accordingly will its ill 3 min read AngularJS Tutorial AngularJS is a free and open-source JavaScript framework that helps developers build modern web applications. It extends HTML with new attributes and it is perfect for single-page applications (SPAs). AngularJS, developed by Google, has been important in web development since its inception in 2009. 5 min read AngularJS | Application Applications in AngularJS enable the creation of real-time Applications. There are four primary steps involved in creation of Applications in AngularJS: Creation of List for an Application. Adding elements in the List. Removing elements from the List. Error Handling Below are the steps for creations 3 min read AngularJS ng-controller Directive The ng-controller Directive in AngularJS is used to add a controller to the application. It can be used to add methods, functions, and variables that can be called on some event like click, etc to perform certain actions. Syntax: <element ng-controller="expression"> Contents... </element 2 min read Angular 7 | Components Components in angular are similar to pages in a website. Components are a key feature in angular. They are well optimized as compared to pages because they are lightweight and reusable in nature. Creating a Component in angular 7: To create a component in any angular application, the first step is t 2 min read AngularJS | API AngularJS APIs are used for comparing, iterating and converting objects.Basic AngularJS API includes angular.isString() angular.lowercase() angular.uppercase() angular.isNumber() 1. angular.isString() It is used to check whether an object is a string or not.It returns true if the object is string ot 2 min read Angular forms NgModel Directive In this article, we are going to see what is NgModel in Angular 10 and how to use it. NgModel is used to create a top-level form group Instance, and it binds the form to the given form value. Syntax: <Input [(NgModel)= 'name']> NgModule: Module used by NgModel is: FormsModule Selectors: [ngMod 1 min read What is the factory function in Angular ? In Angular, the factory function is always inclined towards class and constructors. Generally, the factory function is used to provide service as a dependency in any angular application. A factory function generates an object, provides it with some logic, executes the function, and returns the objec 4 min read Like