Is there any way to assign ng-controller dynamically when iterate over an collection of objects?

134 views Asked by At

Using angular 1.6.6 and ui-bootstrap 2.5.0

I'm trying to use a controller if the action property value is not null.

The logic for each of my tabs is very different so that I don't want to put all tabs in a single controller. Is there any way for this problem?

Actions

$scope.actions = [
    {
        slug: "basicData",
        title: "Grunddata",
        icon: "fa fa-table",
        template: templatePath + "basicData.html",
        disabled: false,
        controller: null
    }, {
        slug: "responsible",
        title: "Ansvariga",
        icon: "fa fa-address-book-o",
        template: templatePath + "responsible.html",
        disabled: false,
        controller: null
    }, {
        slug: "reportTime",
        title: "Rapportera tid",
        icon: "fa fa-clock-o",
        template: templatePath + "reportTime.html",
        disabled: false,
        controller: "ActivityTimeReportingController"
    }

];

Here's my tabset

<uib-tabset active="activePill" vertical="true" type="pills">
            <uib-tab ng-repeat="action in actions" index="$index+1" disable="action.disabled">
                <uib-tab-heading>
                    <span class='{{action.icon}}'></span>&nbsp;{{action.title}}
                </uib-tab-heading>
                <div ng-include="action.template" ng-if="!action.controller" ng-controller="action.controller"></div>
                <div ng-include="action.template" ng-if="action.controller"></div>
            </uib-tab>
        </uib-tabset>

I get the following error

The controller with the name 'action.controller' is not registered.

Curly brackets does not make any difference.

I have tried using a function aswell:

    $scope.getController = function (controllerName) {
    return controllerName.ToString();
};

And in my tabset:

<div ng-include="action.template" ng-if="!action.controller" ng-controller="getController(action.controller)"></div>

With the same error message

The controller with the name 'getController(action.controller' is not registered.

Is there any way to assign ng-controller dynamically when iterate over an collection of objects?

2

There are 2 answers

0
Axar On BEST ANSWER

Create dynamic directives then specify a controller for each, pretty much instead of using ng-include roll your own dynamic directive with binding for template url

http://www.codelord.net/2015/05/19/angularjs-dynamically-loading-directives/

If you dont have too many types of actions.. then use ng-if or ng-switch and create a line for each action with ng-controller or your directive that uses a controller. Dynamic code that you're trying to achieve isn't the right way to do it, imo.

2
krutkowski86 On

Here's a working solution: http://jsfiddle.net/wt42nqLn/

HTML

<script type="text/ng-template" id="customControllerTemplate.html">
  Controller name: {{name}}
</script>

<div ng-controller="myCtrl">
  <div ng-repeat="controller in controllerList">
    <div custom-controller="controller.name">
      <div ng-include="'customControllerTemplate.html'"></div>
    </div>
  </div>
</div>

JS

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

myApp.controller('myCtrl', myCtrl);
myApp.controller('firstController', firstController);
myApp.controller('secondController', secondController);
myApp.directive('customController', customController);

function myCtrl($scope) {
    $scope.controllerList = [
        {
        name: 'firstController'
      },
      {
        name: 'secondController'
      }
    ]
}


function firstController($scope){
    console.log('firstController','loaded');
    $scope.name = 'firstController';
}

function secondController($scope){
    console.log('secondController','loaded');
    $scope.name = 'secondController';
}

function customController($compile){
    return {
        priority:1001, 
        terminal:true, 
        link: function(scope,element,attrs){
           var ctrlName = scope.$eval(attrs['customController']);
           element = angular.element(element.children());
           element.attr('ng-controller',ctrlName);
           $compile(element)(scope);
        }
   };          
}