Search code examples
javascriptangularjscontrollerseparation-of-concerns

Parent and child controllers


Having two controllers, parent and child.

<div ng-controller="firstCtrl as first">
   <div ng-controller="secondCtrl as second"></div>
</div>

JS:

app.controller('firstCtrl', function() {
  this.func = function() {
    //some code
  };
});

app.controller('secondCtrl', function() {
  this.parent.func(some_data);//thats what I need to do   
});

Is it possible to do this without using a factory or $scope.$parent?


Solution

  • Is it possible to do this without using factory or $scope.$parent?

    No, you can't.

    As a side note, I don't really like using $scope.$parent. in a big app, you can really loose the control of your app by imbricating such things.

    If you would like to share a function between controllers, you may want to use a service instead.