简体   繁体   English

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

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

Using angular 1.6.6 and ui-bootstrap 2.5.0 使用角1.6.6和ui-bootstrap 2.5.0

I'm trying to use a controller if the action property value is not null. 如果action属性值不为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. 名称为“action.controller”的控制器未注册。

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. 名为'getController(action.controller')的控制器未注册。

Is there any way to assign ng-controller dynamically when iterate over an collection of objects? 在迭代对象集合时,有没有办法动态分配ng-controller?

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 创建动态指令,然后为每个指定一个控制器,几乎不使用ng-include roll你自己的动态指令和绑定模板url

http://www.codelord.net/2015/05/19/angularjs-dynamically-loading-directives/ 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. 如果您没有太多类型的操作..然后使用ng-if或ng-switch并使用ng-controller或使用控制器的指令为每个操作创建一行。 Dynamic code that you're trying to achieve isn't the right way to do it, imo. 您尝试实现的动态代码不是正确的方法,imo。

Here's a working solution: http://jsfiddle.net/wt42nqLn/ 这是一个有效的解决方案: http//jsfiddle.net/wt42nqLn/

HTML 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 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);
        }
   };          
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM