Is it possible to access a $scope variable defined in the $parent's, $parent scope?
E.g.:
var foo = $scope.$parent.$parent.foo; /* evaluates to undefined */
Is this possible, recommended, there a better alternative?
You should follow dot rule in such cases that will allow you to access the parent scope without having $parent annotation.
If you look at ng-controller API, you will find that the scope: true option which does mean New controller does create a scope which is prototypically inherited from the parent controller, that does allow access to object property which has been already declared in parent scope.
Basically that does follow prototypal inheritance.
Markup
<div ng-controller="myController">
<h1>my Controller Scope Here</h1>
<input type="text" ng-model="myCtrl.data"/>
<div ng-controller="innerController">
{{myCtrl.data}}: {{innerCtrl}}
</div>
</div>
Controller
app.controller('myController', function($scope){
$scope.myCtrl = {};
})
app.controller('innerController', function($scope){
$scope.innerCtrl = 'inner Data';
})
scope:true or abstract:true ?abstract: true..there is no such property exists in directive API..are you ok?angular ui-router$parent
$parent, the more spaghetti code you end up with. nested controllers are generally something you should try to avoid whenever possible. Imagine reading someone's code and trying to figure out where$scope.$parent.$parent.$parent.apropertyis actually defined.controllerA as ctrlA,controllerB as ctrlB, etc.. Then you can begin to separate the nesting by referring to each property in terms of the controller that it belongs to, i.e.ctrlA.property,ctrlB.property; you'll likely find a property or two with name collisions, but you'll be able to more easily identify what is going on with each property, and might find the nesting isn't even necessary.$scope, and thus properties added to the controller can be referenced throughthisin the controller. but what that means is, if you reference the$scope.$parent, it might actually be$scope.$parent.someController.somePropertythat you are trying to reach.