I am new to angularjs and got a confusing thing which I thought is probably an anomaly in controller hierarchy in accessing parent scope. Below are the two code snippets, I think both must work according to the principle of accessing parent scope but...
1) This one is working:
<body data-ng-app>
<div data-ng-controller="ctrl1">
Greeted : {{first.greeted}}
<div data-ng-controller="ctrl2">
<a href="#" data-ng-click="sayClick()">Click To Greet</a>
</div>
</div>
<script>
function ctrl1($scope){
$scope.first = {greeted: "No"};
}
function ctrl2($scope){
$scope.sayClick= function(){
$scope.first.greeted = "Yes";
}
}
</script>
</body>
2) and this one is not:
<body data-ng-app>
<div data-ng-controller="ctrl1">
Greeted : {{first}}
<div data-ng-controller="ctrl2">
<a href="#" data-ng-click="sayClick()">Click To Greet</a>
</div>
</div>
<script>
function ctrl1($scope){
$scope.first = "No";
}
function ctrl2($scope){
$scope.sayClick= function(){
$scope.first = "Yes";
}
}
</script>
</body>
Could someone please tell me whats the difference between the two and why second one isn't working?