AngularJS: dynamically assign controller from ng-repeat

I’m trying to dynamically assign a controller for included template like so:

<section ng-repeat="panel in panels">
    <div ng-include="'path/to/file.html'" ng-controller="{{panel}}"></div>

But Angular complains that {{panel}} is undefined.

I’m guessing that {{panel}} isn’t defined yet (because I can echo out {{panel}} inside the template).

I’ve seen plenty of examples of people setting ng-controller equal to a variable like so: ng-controller="template.ctrlr". But, without creating a duplicate concurrant loop, I can’t figure out how to have the value of {{panel}} available when ng-controller needs it.

P.S. I also tried setting ng-controller="{{panel}}" in my template (thinking it must have resolved by then), but no dice.

Your problem is that ng-controller should point to controller itself, not just string with controller’s name.

So you might want to define $scope.sidepanels as array with pointers to controllers, something like this, maybe:

$scope.sidepanels = [Alerts, Subscriptions];

Here is the working example on js fiddle

However, i find very weird all this situation when you might want to set up controllers in ngRepeat.

To dynamically set a controller in a template, it helps to have a reference to the constructor function associated to a controller. The constructor function for a controller is the function you pass in to the controller() method of Angular’s module API.

Having this helps because if the string passed to the ngController directive is not the name of a registered controller, then ngController treats the string as an expression to be evaluated on the current scope. This scope expression needs to evaluate to a controller constructor.

For example, say Angular encounters the following in a template:


If no controller with the name myController is registered, then Angular will look at $scope.myController in the current containing controller. If this key exists in the scope and the corresponding value is a controller constructor, then the controller will be used.

Read More:   jQuery: Return data after ajax call success [duplicate]

This is mentioned in the ngController documentation in its description of the parameter value: “Name of a globally accessible constructor function or an expression that on the current scope evaluates to a constructor function.” Code comments in the Angular source code spell this out in more detail here in src/ng/controller.js.

By default, Angular does not make it easy to access the constructor associated to a controller. This is because when you register a controller using the controller() method of Angular’s module API, it hides the constructor you pass it in a private variable. You can see this here in the $ControllerProvider source code. (The controllers variable in this code is a variable private to $ControllerProvider.)

My solution to this issue is to create a generic helper service called registerController for registering controllers. This service exposes both the controller and the controller constructor when registering a controller. This allows the controller to be used both in the normal fashion and dynamically.

Here is code I wrote for a registerController service that does this:

var appServices = angular.module('', []);

// Define a registerController service that creates a new controller
// in the usual way.  In addition, the service registers the
// controller's constructor as a service.  This allows the controller
// to be set dynamically within a template.
appServices.config(['$controllerProvider', '$injector', '$provide',
  function ($controllerProvider, $injector, $provide) {
      function registerControllerFactory() {
        // Params:
        //   constructor: controller constructor function, optionally
        //     in the annotated array form.
        return function registerController(name, constructor) {
            // Register the controller constructor as a service.
            $provide.factory(name + 'Factory', function () {
                return constructor;
            // Register the controller itself.
            $controllerProvider.register(name, constructor);

Here is an example of using the service to register a controller:['registerController',
  function (registerController) {

    registerController('testCtrl', ['$scope',
      function testCtrl($scope) {
        $ = 'bar';


The code above registers the controller under the name testCtrl, and it also exposes the controller’s constructor as a service called testCtrlFactory.

Now you can use the controller in a template either in the usual fashion–


or dynamically–


For the latter to work, you must have the following in your current scope:

$scope.templateController = testCtrlFactory

I believe you’re having this problem because you’re defining your controllers like this (just like I’m used to do):

app.controller('ControllerX', function() {
    // your controller implementation        

If that’s the case, you cannot simply use references to ControllerX because the controller implementation (or ‘Class’, if you want to call it that) is not on the global scope (instead it is stored on the application $controllerProvider).

Read More:   Handling unexpected side effect in computed properties - VueJS

I would suggest you to use templates instead of dynamically assign controller references (or even manually create them).


var app = angular.module('app', []);    
app.controller('Ctrl', function($scope, $controller) {
    $scope.panels = [{template: 'panel1.html'}, {template: 'panel2.html'}];        

app.controller("Panel1Ctrl", function($scope) {
    $ = 1;
app.controller("Panel2Ctrl", function($scope) {
    $ = 2;

Templates (mocks)

<!-- panel1.html -->
<script type="text/ng-template" id="panel1.html">
  <div ng-controller="Panel1Ctrl">
    Content of panel {{id}}

<!-- panel2.html -->
<script type="text/ng-template" id="panel2.html">
  <div ng-controller="Panel2Ctrl">
    Content of panel {{id}}


<div ng-controller="Ctrl">
    <div ng-repeat="panel in panels">
        <div ng-include src=""></div>        


Another way is to not use ng-repeat, but a directive to compile them into existence.




angular.module('app.directives', [])
    .directive('mysections', ['$compile', function(compile){
        return {
            restrict: 'E',
            link: function(scope, element, attrs) {
                for(var i=0; i<panels.length; i++) {
                    var template="<section><div ng-include="path/to/file.html" ng-controller=""+panels[i]+'"></div></section>';
                    var cTemplate = compile(template)(scope);


Ok I think the simplest solution here is to define the controller explicitly on the template of your file. Let’s say u have an array:

$scope.widgets = [
      {templateUrl: 'templates/widgets/aWidget.html'},
      {templateUrl: 'templates/widgets/bWidget.html'},

Then on your html file:

<div ng-repeat="widget in widgets">
    <div ng-include="widget.templateUrl"></div>

And the solution aWidget.html:

<div ng-controller="aWidgetCtrl">


<div ng-controller="bWidgetCtrl">

Simple as that! You just define the controller name in your template. Since you define the controllers as bmleite said:

app.controller('ControllerX', function() {
// your controller implementation        

then this is the best workaround I could come up with. The only issue here is if u have like 50 controllers, u’ll have to define them explicitly on each template, but I guess u had to do this anyway since you have an ng-repeat with controller set by hand.

Read More:   Command-click doesn't open a new tab, but middle-click does

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts