I am learning AngularJS. I am confused about the inheritance of $scope object. Like i hope $scope object is specific to a controller. How it inherits values to the other controllers. Code below is taken from official documentation
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - example-example9-production</title>
<link href="app.css" rel="stylesheet" type="text/css">
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-rc.0/angular.min.js"></script>
<script src="app.js"></script>
</head>
<body ng-app="scopeInheritance">
<div class="spicy">
<div ng-controller="MainController">
<p>Good {{timeOfDay}}, {{name}}!</p>
<div ng-controller="ChildController">
<p>Good {{timeOfDay}}, {{name}}!</p>
<div ng-controller="GrandChildController">
<p>Good {{timeOfDay}}, {{name}}!</p>
</div>
<div ng-controller="GrandChildController2">
<p>Good {{timeOfDay}}, {{name}}!</p>
</div>
</div>
</div>
</div>
</body>
</html>
App.js
(function(angular) {
'use strict';
var myApp = angular.module('scopeInheritance', []);
myApp.controller('MainController', ['$scope', function($scope) {
$scope.timeOfDay = 'morning';
$scope.name = 'Nikki';
}]);
myApp.controller('ChildController', ['$scope', function($scope) {
$scope.timeOfDay = 'morning2';
$scope.name = 'Mattie';
}]);
myApp.controller('GrandChildController', ['$scope', function($scope) {
$scope.timeOfDay = 'evening';
$scope.name = 'Gingerbread Baby';
}]);
myApp.controller('GrandChildController2', ['$scope', function($scope) {
}]);
})(window.angular);
The GrandChildController2 controller did not change the name and timeOfDay property of $scope object. I expect the output should be like
timeOfDay ='evening' and name = 'Gingerbread Baby' if it inherits from parent. But surprisingly the output of GrandChildController2 controller is timeOfDay ='Good morning2' and name ='Mattie'. How is this possible?
Could you please explain
Thanks Manivannan