Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS access parent scope from child controller

I've set up my controllers using data-ng-controller="xyzController as vm"

I have a scenario with parent / child nested controllers. I have no problem accessing parent properties in the nested html by using $parent.vm.property, but I cannot figure out how to access the parent property from within my child controller.

I've tried injecting $scope and then using $scope.$parent.vm.property, but this isn't working?

Can anyone offer advice?

like image 341
zpydee Avatar asked Jan 30 '14 10:01

zpydee


People also ask

How do you access child controller scope in parent controller?

In a nutshell: You cannot access child scopes from a parent scope. Your solutions: Define properties in parents and access them from children (read the link above) Use a service to share state.

Can parent controller access the methods of child controller or vice versa?

39) Do you think that parent controller can access the methods of child controller or vice versa? No, the parent controller cannot access the methods of child controller, but the child controller can access the methods of the parent controller.

What is parent scope in AngularJS?

Angular scopes include a variable called $parent (i.e. $scope. $parent ) that refer to the parent scope of a controller. If a controller is at the root of the application, the parent would be the root scope ( $rootScope ). Child controllers can therefore modify the parent scope since they access to it.

How do you access parent scope in directive?

You can still access the parent scope using $parent , but this is not normally recommended. Instead, you should specify which parent scope properties (and/or function) the directive needs via additional attributes on the same element where the directive is used, using the = , @ , and & notation.


1 Answers

If your HTML is like below you could do something like this:

<div ng-controller="ParentCtrl">     <div ng-controller="ChildCtrl">     </div> </div> 

Then you can access the parent scope as follows

function ParentCtrl($scope) {     $scope.cities = ["NY", "Amsterdam", "Barcelona"]; }  function ChildCtrl($scope) {     $scope.parentcities = $scope.$parent.cities; } 

If you want to access a parent controller from your view you have to do something like this:

<div ng-controller="xyzController as vm">    {{$parent.property}} </div> 

See jsFiddle: http://jsfiddle.net/2r728/

Update

Actually since you defined cities in the parent controller your child controller will inherit all scope variables. So theoritically you don't have to call $parent. The above example can also be written as follows:

function ParentCtrl($scope) {     $scope.cities = ["NY","Amsterdam","Barcelona"]; }  function ChildCtrl($scope) {     $scope.parentCities = $scope.cities; } 

The AngularJS docs use this approach, here you can read more about the $scope.

Another update

I think this is a better answer to the original poster.

HTML

<div ng-app ng-controller="ParentCtrl as pc">     <div ng-controller="ChildCtrl as cc">         <pre>{{cc.parentCities | json}}</pre>         <pre>{{pc.cities | json}}</pre>     </div> </div> 

JS

function ParentCtrl() {     var vm = this;     vm.cities = ["NY", "Amsterdam", "Barcelona"]; }  function ChildCtrl() {     var vm = this;     ParentCtrl.apply(vm, arguments); // Inherit parent control      vm.parentCities = vm.cities; } 

If you use the controller as method you can also access the parent scope as follows

function ChildCtrl($scope) {     var vm = this;     vm.parentCities = $scope.pc.cities; // note pc is a reference to the "ParentCtrl as pc" } 

As you can see there are many different ways in accessing $scopes.

Updated fiddle

function ParentCtrl() {      var vm = this;      vm.cities = ["NY", "Amsterdam", "Barcelona"];  }        function ChildCtrl($scope) {      var vm = this;      ParentCtrl.apply(vm, arguments);            vm.parentCitiesByScope = $scope.pc.cities;      vm.parentCities = vm.cities;  }      
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.20/angular.min.js"></script>  <div ng-app ng-controller="ParentCtrl as pc">    <div ng-controller="ChildCtrl as cc">      <pre>{{cc.parentCities | json}}</pre>      <pre>{{cc.parentCitiesByScope | json }}</pre>      <pre>{{pc.cities | json}}</pre>    </div>  </div>
like image 52
Dieterg Avatar answered Sep 28 '22 10:09

Dieterg