The $scope object is the most vital bridge in an AngularJS application.
It is the magical glue that binds the Controller logic directly to the HTML View.
Think of $scope as an empty bucket created specifically for a controller.
You drop variables, arrays, and functions into this bucket from your JavaScript.
Your HTML expressions and directives then reach into this bucket to display the data!
Every application has a single master scope known as $rootScope.
All other $scope objects are child scopes derived from this master root.
If a variable is created on $rootScope, it is available to every single component in the app.
<div ng-app="myApp" ng-controller="myCtrl">
<h1>{{ greeting }}</h1>
<button ng-click="changeGreeting()">Change Greeting</button>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.greeting = "Hello, World!";
$scope.changeGreeting = function() {
$scope.greeting = "Welcome to AngularJS!";
};
});
</script>
Properly managed scopes prevent data collision and ensure clean execution.
Avoid dumping too many variables into the global $rootScope to maintain high performance.
Fast, bug-free applications are significantly favored by modern search engine algorithms.
What is the name of the master scope that is available globally to all controllers?