I have a view like follow :
<html ng-app="app" ng-controller="mainController">[...]
<body ng-controller="currentController">[...]
I'd like to be able to set the body controller from a variable ($scope.currentController) of an upper controller (mainController). The controller mainController looks like follow :
angular.module("app").controller("mainController", function($scope) {
$scope.currentController = "indexController"; // 'indexController' being the name of the controller I want to apply to the view
});
This doesn't work obviously, how can I set a controller dynamically from JS to the view ?
EDIT : (to @mrhobo answer)
I tried this, giving the fact that I have one file per controller :
angular.module("app").controller("mainController", function($scope, indexController) {
$scope.currentController = indexController;
});
I got this error : Doc Angular error
mainControllerto thehtmltag instead to achieve a parent-child relationship?indexControllerfor the home page orappControllerfor the application page. This two controllers do not manage the same things. I've tried to put them both onmainController, it works fine, but I have errors on my console because I have references to elements that doesn't exist in each one of them. That's why I'd prefer thatmainControllerchoose the one to apply depending on the page used. But in the code shown, I just show the basic idea of what I'd like to do : set a controller from JS using its name.mainControllerto thehtmltag so that the controller on body is a child controller tomainController. Let me know if this is acceptable or not.