Angular嵌套控制器对Child的引用

时间:2016-08-19 23:49:40

标签: angularjs

我有两个控制器:TaskManageControllerTaskFormControllerTaskFormController具有呈现它的功能(模态),而TaskManageController具有" +"按钮添加。目前,TaskManageControllerTaskFormController的父级。单击" +"时,如何在子控制器上调用present?父控制器中的按钮?

<div ng-controller="TaskManageController AS manage">
  ...
  <a ng-click="manage.add()">+</a>
  <div ng-controller="TaskFormController AS form">
    ...
  </div>
</div>

1 个答案:

答案 0 :(得分:1)

您可以使用ng-init设置对子控制器的引用,然后在管理控制器中使用该引用:

<div ng-controller="TaskManageController AS manage">
  ...
  <a ng-click="manage.add()">+</a>
  <div ng-controller="TaskFormController AS form" ng-init="manage.form = form">
    ...
  </div>
</div>

// TaskManageContoller
this.add = function() {
  if (this.form) { this.form.present(); }
}