How to share data between controllers in AngularJS ? Last Updated : 14 Feb, 2023 Comments Improve Suggest changes Like Article Like Report The task is to share data variables between two or more controllers by using AngularJS. There are many procedures to achieve this. Here we will discuss the most popular ones. Approach: To share data between the controllers in AngularJS we have two main cases: Share data between parent and child: Here, the sharing of data can be done simply by using controller inheritance as the scope of a child controller inherits from the scope of the parent controller.Share data between controllers without having a relation: Here, the sharing of data can be done in a few ways some of them are: By using the rootScope variable: We can use the rootScope variable to hold shared data and then can reference it from any controller. Here, at the start of the Angular app, we initialized the rootScope variable with some value and then refer it from every controller and thus binding scope variables in both controllers to the rootScope variable. Example 1: This example demonstrates the basic sharing of the data between controllers in AngularJS by using the rootScope variable. HTML <!DOCTYPE html> <html> <head> <title> Angular JS sharing data using rootScope </title> <script src= "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"> </script> </head> <body> <h1 style="color:green;"> GeeksforGeeks </h1> <div ng-app="mainApp"> <div ng-controller="firstcontroller"> <h2>First controller</h2> <p>{{firstvalue}}</p> </div> <div ng-controller="secondcontroller"> <h2>Second controller</h2> <p>{{secondvalue}}</p> </div> </div> <script> var mainApp = angular.module("mainApp", []); mainApp.run(function ($rootScope) { $rootScope.value = 'A Computer Science Portal for Geeks'; }); mainApp.controller('firstcontroller', function ($scope, $rootScope) { $scope.firstvalue = $rootScope.value; }); mainApp.controller('secondcontroller', function ($scope, $rootScope) { $scope.secondvalue = $rootScope.value; }); </script> </body> </html> Output: By using factory or service: The $rootscope method is not preferred for data transfer or sharing data because it has a global scope that is available for the entire application. So, we use another method in which we create a factory or service to hold share data. AngularJS factories and services are JS functions that perform a specific task containing both methods & properties and can be injected into other components (e.g. your controllers) using dependency injection. In this way we can define a shared variable in a factory, inject it into both controllers and thus bind scope variables in both controllers to this factory data. Example 2: This example demonstrates the sharing of the data between controllers in AngularJS by using factory or service. HTML <!DOCTYPE html> <html> <head> <title> Angular JS sharing data using factory </title> <script src= "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"> </script> </head> <body> <h1 style="color:green;"> GeeksforGeeks </h1> <div ng-app="mainApp"> <div ng-controller="FirstController"> <input type="text" ng-model="value.name"><br> Input in first controller is : {{value.name}} </div><hr> <div ng-controller="SecondController"> Input in second controller is : {{value.name}} </div> </div> <script> var mainApp = angular.module("mainApp", []); mainApp.factory('Fact', function () { return { name: '' }; }); mainApp.controller('FirstController', function ($scope, Fact) { $scope.value = Fact; }); mainApp.controller('SecondController', function ($scope, Fact) { $scope.value = Fact; }); </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How to share data between controllers in AngularJS ? P pratyushranjan14 Follow Improve Article Tags : Technical Scripter Web Technologies AngularJS Technical Scripter 2019 AngularJS-Questions +1 More Similar Reads How to create nested controllers in Angular.js ? A controller in AngularJS is a JavaScript object created with the help of a JavaScript object constructor. A controller can contain properties and functions. Controllers are used for controlling the application data of an AngularJS application. In this article, we will see the nested controllers in 4 min read How to use filter within controllers in AngularJS ? In this article, we will see how to use the Filter inside the controller using AngularJS. Filters are used to format the value of an expression and display it to the user. It can be used in HTML Previews, Controllers or Services, and directives. AngularJS facilitates many built-in filters, although, 4 min read How to set, get and clear cookies in AngularJS ? In this article, we will see how to set, get & clear the Cookies in AngularJS, along with understanding the basic usage through the implementation. In AngularJS, we need to use angular-cookies.js to set, get and clear the cookies. You can use the live CDN link for this: https://cdnjs.cloudflare. 2 min read How to create the dynamic forms in AngularJS ? In AngularJS, we need to create dynamic forms to make the user interact. We can create and apply different behaviors to the forms to represent and accept the data from the user. We can create dynamic forms using ng-repeat and ng-show in AngularJS. In this article, we will see how we can create dynam 6 min read How to establish communication between independent components in AngularJS ? AngularJS is a popular open-source web application framework that allows developers to create dynamic and interactive web applications. One of the key features of AngularJS is its ability to allow independent components to communicate with each other. In this article, we will see the process of esta 4 min read How To Share Data Between Child And Parent Directives And Components In Angular? Sharing data between child and parent components in Angular is important for building dynamic and interactive applications. This can be done through various concepts, including input and output bindings or using a shared service. This article will explore how to use an Angular service to enable data 5 min read How many types of data bindings in AngularJS ? In this article, we will know about the concept of data binding, along with different types of binding available in AngularJS. The Data Binding refers to the synchronization of data between the model and view. Synchronizing data is imperative for keeping the data being displayed to the user and the 6 min read How to execute AngularJS controller function on page load ? In this article, we will see how to execute/call a JS function on page load using AngularJS. This function can be used to perform initialization. Calling a function or initializing a single value on page load in AngularJS is quite easy. AngularJS provides us with a dedicated directive for this speci 2 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 How to get file content and other details in AngularJS? We can get the file content by using some basic angular functions and other details like the name and size of the file in AngularJS. To understand it look into the below example where both HTML and JS files are implemented. Note: Consider below two files are of same component in angular. app.module. 2 min read Like