使用Angular 1.6.6和UI-Bootstrap 2.5.0
如果操作属性值不是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"
}
];
这是我的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> {{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"名称的控制器。
卷曲支架没有任何区别。
我尝试使用一个函数ASWell:
$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?
创建动态指令,然后为每个指令指定一个控制器,而不是使用ng-include滚动您自己的动态指令,并绑定模板url
http://www.codelord.net/2015/05/19/angularjs-dynamaly-loading-directives/
如果您没有太多类型的操作。.然后使用ng-if或ng-switch,并使用ng-controller或使用控制器的指令为每个动作创建一条线。您要实现的动态代码不是正确的方法。
这是一个工作解决方案: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);
}
};
}