简体   繁体   中英

How to access parent controller data inside child controller in angular js

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?

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>

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM