How can I use multiple angularjs's controller inside a view (or subview of a multi-views)?

507 Views Asked by At

As title, I'd like to use more than one controller inside some sub-view

routes.js (eg.):

.state("multi",{
            url: "/multi",
            views: {
                "": {
                    templateUrl: "multipleView.htm",
                    controller: "MainCTRL",
                    controllerAs: "ctrl"
                },
                "viewA@multi": {
                    templateUrl: "testingBlock.htm",
                    controller: ["CtrlOne", "CtrlTwo"],
                    controllerAs: "ctrl"
                },
            });

Or should I put CtrlOne and CtrlTwo inside a third controller:

function CtrlThree($scope){
         CtrlOne($scope);
         CtrlOTwo($scope);
}
1

There are 1 best solutions below

0
Phil Kearney On BEST ANSWER

Why not put the controller in the view instead of specifying it on the routeProvider.

Also do the controllers have a hierarchy or do you want them to all just work on the same level.

Example of what I mentioned above.

<div ng-controller="CtrlOne"> <div ng-controller="CtrlTwo"></div> </div>

This would be the html inside the "multipleView.htm" file.