迭代一组对象时,有没有办法动态分配ng-controller?

时间:2017-12-13 13:21:21

标签: angularjs angularjs-ng-repeat ng-controller angular-ui-bootstrap-tab

使用angular 1.6.6和ui-bootstrap 2.5.0

如果action属性值不为null,我正在尝试使用控制器。

我的每个标签的逻辑都非常不同,因此我不想将所有标签放在一个控制器中。有没有办法解决这个问题?

操作

$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"
    }

];

这是我的标签

<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>

我收到以下错误

  

名称为“action.controller”的控制器未注册。

圆括号没有任何区别。

我尝试过使用函数:

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

在我的标签栏中:

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

使用相同的错误消息

  

名为'getController的控制器(action.controller'未注册。

在迭代对象集合时,有没有办法动态分配ng-controller?

2 个答案:

答案 0 :(得分:1)

创建动态指令然后为每个指定一个控制器,而不是使用ng-include roll你自己的动态指令和绑定模板url

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

如果您没有太多类型的操作..然后使用ng-if或ng-switch并使用ng-controller或使用控制器的指令为每个操作创建一行。您尝试实现的动态代码不是正确的方法,imo。

答案 1 :(得分:0)

这是一个有效的解决方案: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);
        }
   };          
}