culturalanomoly
culturalanomoly

Reputation: 1301

AngularJS Nested Scope/View

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?

Upvotes: 0

Views: 1718

Answers (1)

Pankaj Parkar
Pankaj Parkar

Reputation: 136144

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';
})

Upvotes: 1

Related Questions