2017-08-10 135 views
0

我有孩子的應用程序控制器上覆選框,所以當它的用戶點擊,我需要調用從父控制器的一些方法: 父應用程序控制器:調用父類的方法

<div ng-controller="ParentCntr as parentCntr"> 
    <child-app></child-app> 
</div> 

兒童應用:

<div class="child-app" ng-controller="ChildCntr as childCntr"> 
     <div class="checkbox no-indent"> 
      <input type="checkbox" 
       name="test" 
       id="test" 
       ng-change="childCntr.DisableText()" 
       ng-model="childCntr.testVar"/> 
     <label for="test">Some Text</label> 
     </div> 
    </div> 

那麼,如何以最簡單的方式做到這一點?

+2

嘗試'$ parent.yourFunction()' – JeanJacques

+4

ng-click =「parentCntr.myMethod()」? –

+1

[AngularJS從子控制器訪問父範圍]的可能副本(https://stackoverflow.com/questions/21453697/angularjs-access-parent-scope-from-child-controller) – Vivz

回答

0

因此,要訪問父範圍可以使用$父這樣的:

<div ng-controller='SecondCtrl' ng-click='$parent.testFunction()'>{{$parent.name}}</div> 

Here是一個工作的例子。