Stussa
Stussa

Reputation: 3415

Angular Nested Controller Reference to Child

I've got two controllers: TaskManageController and TaskFormController. The TaskFormController has function to present it (modally) and the TaskManageController has an "+" button to add. Currently the TaskManageController is a parent of the TaskFormController. How can I call present on the child controller when clicking the "+" button in the parent controller?

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

Upvotes: 1

Views: 24

Answers (1)

Kevin Sylvestre
Kevin Sylvestre

Reputation: 38032

You could use ng-init to setup a reference to the child controller then use the reference in your manage controller:

<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(); }
}

Upvotes: 1

Related Questions