I was trying to learn controller inheritance in angularJS.
Please follow the code below
<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
<title>Recipe 02 example 01</title>
<script type="text/javascript" src="D:\Rahul Shivsharan\JavaScript-Framework\AngularJS\angular.js"></script>
<script type="text/javascript" src="D:\Rahul Shivsharan\JavaScript-Framework\jQuery\jquery-1.11.1.js"></script>
<link rel="stylesheet" href="D:\Rahul Shivsharan\JavaScript-Framework\BootstrapCSS\bootstrap-3.2.0-dist\css\bootstrap.css"></link>
<script type="text/javaScript">
angular.module("myApp",[]);
(function(){
angular.module("myApp").controller("ParentCtrl",ParentCtrl);
angular.module("myApp").controller("ChildCtrl",ChildCtrl);
function ParentCtrl($scope){
$scope.lastName = "Bond"
};
function ChildCtrl($scope){
$scope.firstName = "James"
};
})();
</script>
</head>
<body>
<div ng-controller="ParentCtrl">
<div ng-controller="ChildCtrl">
<h3>Full name is {{firstName + " "+ lastName}}</h3>
</div>
</div>
</body>
The above code prints the output
Full name is James Bond
But
If I give the alias name to the controllers, it doesn't work, as seen in below code
<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
<title>Recipe 02 example 01</title>
<script type="text/javascript" src="D:\Rahul Shivsharan\JavaScript-Framework\AngularJS\angular.js"></script>
<script type="text/javascript" src="D:\Rahul Shivsharan\JavaScript-Framework\jQuery\jquery-1.11.1.js"></script>
<link rel="stylesheet" href="D:\Rahul Shivsharan\JavaScript-Framework\BootstrapCSS\bootstrap-3.2.0-dist\css\bootstrap.css"></link>
<script type="text/javaScript">
angular.module("myApp",[]);
(function(){
angular.module("myApp").controller("ParentCtrl",ParentCtrl);
angular.module("myApp").controller("ChildCtrl",ChildCtrl);
function ParentCtrl(){
var obj = this;
obj.lastName= "Bond"
};
function ChildCtrl(){
var obj = this;
obj.firstName = "James"
};
})();
</script>
</head>
<body>
<div ng-controller="ParentCtrl as p">
<div ng-controller="ChildCtrl as c">
<h3>Full name is {{c.firstName + " "+ c.lastName}}</h3>
</div>
</div>
</body>
The output is
Full name is James
What I assumed was there is a prototype chaining from ChildCtrl to ParentCtrl. something like
ChildCtrl.prototype = new ParentCtrl();
var c = new ChildCtrl();
console.log(" Full name is "+c.firstName+" "+c.lastName)
Is it not so ?
Please explain me or give me some pointers.