0

Can we access model data of parent controller inside child controller

<div ng-controller="abc">
    <div ng-controller="def">
        <span> {{name}}</span>
    </div>
</div>

can we access "model" value is it belongs in "abc" controller?

2 Answers2

0

Below code will help you. you can be able to access parent controller using $parent.

<script>
    function abc($scope) {
        $scope.name = "Pragnesh"
    }
    function def($scope) {
        $scope.name = "Test"
    }
</script>
<html ng-app="">
<body>
    <div ng-controller="abc">
       <div ng-controller="def">
          <span> {{name}}</span>
          <span> {{$parent.name}}</span>
       </div>
    </div>
</body>

Pragnesh Khalas
  • 2,908
  • 2
  • 13
  • 26
0

Try using $parent (i.e. $scope.$parent), this will refer to the parent scope of a controller.

It is also explained here: AngularJS access parent scope from child controller

Community
  • 1
  • 1
Dakshal Raijada
  • 1,261
  • 8
  • 18