I would like to understand why when using a ng-repeat
with a certain controller on the item of the repeat, the parent of that item and the grandfather of that item are the same controller. I was expecting the grandfather to be the parent of the parent controller, not the same controller.
Here the code to clarify this
HTML
<div ng-controller="MainController">
{{name}}
<div ng-controller="SecondController">
{{name}}
<ul>
<li ng-repeat="item in list" ng-controller="ItemController">
{{item.name}} {{$parent.name}} {{myDad}} {{myGrandpa}}
</li>
</ul>
<div ng-controller="ThirdController">
{{name}} {{$parent.name}} {{myDad}} {{myGrandpa}}
</div>
</div>
</div>
JS
angular.module('app', []);
angular.module('app')
.controller('MainController', function($scope) {
$scope.name = "MainController";
})
.controller('SecondController', function($scope) {
$scope.name = "SecondController";
$scope.list = [
{'name': 'aaa'}
];
})
.controller('ItemController', function($scope) {
$scope.name = "ItemController";
$scope.myDad = $scope.$parent.name;
$scope.myGrandpa = $scope.$parent.$parent.name;
})
.controller('ThirdController', function($scope) {
$scope.name = "ThirdController";
$scope.myDad = $scope.$parent.name;
$scope.myGrandpa = $scope.$parent.$parent.name;
});
Here a CodePen