KEMBAR78
What is $root scope in angularjs | DOCX
What is $rootScope in
AngularJs
 $rootScope is a parent scope of all $scope and can be sharedtoall $scope.
 One applicationcanhave onlyone $rootScope.
 $scope is a JavaScriptobjectassociatedtocontroller but$rootScope isnot.
 Scopesprovide separationbetweenmodel andthe view.
 For use of $rootScope needtoinjectintocontroller.
 Methodsand properties createdinthe $scope objectare onlyaccessed insidesame controllerin
view.
 All otherscopesare descendantscopesof the rootscope.
<!DOCTYPE html>
<html>
<head>
<title>Share data between controllers in AngularJs with $rootScope</title>
<link rel="stylesheet"
href="http://getbootstrap.com/2.3.2/assets/css/bootstrap.css">
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.4/angular.js"></scri
pt>
<script>
var app = angular.module("myApp", []);
app.run(function($rootScope) {
$rootScope.userData = {};
$rootScope.userData.firstName = "Ravi";
$rootScope.userData.lastName = "Sharma";
});
app.controller("firstController", function($scope, $rootScope) {});
app.controller("secondController", function($scope, $rootScope) {});
</script>
</head>
<body ng-app="myApp">
<div ng-controller="firstController">
<br>
<input type="text" ng-model="userData.firstName">
<br>
<input type="text" ng-model="userData.lastName">
<br>
<br>First Name: <strong>{{userData.firstName}}</strong>
<br>Last Name : <strong>{{userData.lastName}}</strong> </div>
<hr>
<div ng-controller="secondController"> Showing first name and last name on
second controller: <b>
{{userData.firstName}} {{userData.lastName}}</b> </div>
</body>
</html>
Thanks
www.codeandyou.com
http://www.codeandyou.com/2015/09/what-is-
rootscope-in-angularjs.html
Keywords - What is $rootScope in AngularJs, $rootScope, AngularJs
$rootScope

What is $root scope in angularjs

  • 1.
    What is $rootScopein AngularJs
  • 2.
     $rootScope isa parent scope of all $scope and can be sharedtoall $scope.  One applicationcanhave onlyone $rootScope.  $scope is a JavaScriptobjectassociatedtocontroller but$rootScope isnot.  Scopesprovide separationbetweenmodel andthe view.  For use of $rootScope needtoinjectintocontroller.  Methodsand properties createdinthe $scope objectare onlyaccessed insidesame controllerin view.  All otherscopesare descendantscopesof the rootscope.
  • 3.
    <!DOCTYPE html> <html> <head> <title>Share databetween controllers in AngularJs with $rootScope</title> <link rel="stylesheet" href="http://getbootstrap.com/2.3.2/assets/css/bootstrap.css"> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.4/angular.js"></scri pt> <script> var app = angular.module("myApp", []); app.run(function($rootScope) { $rootScope.userData = {}; $rootScope.userData.firstName = "Ravi"; $rootScope.userData.lastName = "Sharma"; }); app.controller("firstController", function($scope, $rootScope) {}); app.controller("secondController", function($scope, $rootScope) {}); </script> </head> <body ng-app="myApp"> <div ng-controller="firstController"> <br> <input type="text" ng-model="userData.firstName"> <br> <input type="text" ng-model="userData.lastName"> <br> <br>First Name: <strong>{{userData.firstName}}</strong> <br>Last Name : <strong>{{userData.lastName}}</strong> </div> <hr> <div ng-controller="secondController"> Showing first name and last name on second controller: <b> {{userData.firstName}} {{userData.lastName}}</b> </div> </body> </html>
  • 4.