简体   繁体   English

如何在Angular JS的子控制器中访问父控制器数据

[英]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? 我们可以访问“模型”值,它是否属于“ abc”控制器?

Below code will help you. 以下代码将为您提供帮助。 you can be able to access parent controller using $parent. 您可以使用$ 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. 尝试使用$ parent(即$ scope。$ parent),这将引用控制器的父作用域。

It is also explained here: AngularJS access parent scope from child controller 在此也进行了说明: AngularJS从子控制器访问父作用域

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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