There isn't an easy way to do what you are asking. Ctrl1 isn't defined in the scope of MainController, it's in a sub-scope. Ctrl1 and Ctrl2 aren't even in the same scopes as each other so there's no real reason why you need to use different names. Also you could have ng-repeat or ng-if creating and destroying ControllerA instances so the lifetimes may be shorter than for MainController.
A better way to handle this would be to invert the responsiblities and make ControllerA instances register with MainController when created and de-register when destroyed. If you use controllerAs syntax also for MainController then it is easy for the child controllers to call methods on MainController.
<div ng-controller="MainController as Main">
<div ng-controller="ControllerA as Ctrl1" ng-init="Ctrl1.init('Ctrl1')"></div>
<div ng-controller="ControllerA as Ctrl2" ng-init="Ctrl2.init('Ctrl2')"></div>
</div>
function ControllerA($scope) {
const vm = this;
vm.init = function(name) {
$scope.Main.register(name, vm);
$scope.$on('$destroy', function() {
$scope.Main.deregister(name);
});
}
}
You then write suitable register and deregister functions in MainController to keep track of the child controllers. Something like:
function MainController() {
const vm = this;
const children = {};
vm.register = function(name, vm) { children[name] = vm; }
vm.deregister = function(name) { delete children[name]; }
// ... and now you can do
if (children.Ctrl1) children.Ctrl1.controllerFunction();
}
You might instead put the init method in MainController but that would make de-registration difficult as you need the child scope.
ng-showcurrently). In one of that view the controller is placed in a modal dialog while on the other it is placed embedded on the page. Now when I want to load data within the controller (always in just one instance), I can't get that managed. Using$broadcast, both controllers will get active. I want to call a certain controller depending on the method of the parent controller.