I've run into a problem in a tab/panel directive I'm trying to make. I need to be able to supply a template and a controller to each panel, either as a string in the HTML or as an expression to be bound. E.g. I need to be able to call it like this...
<tab-panel tab-id="tab1"
template="myTemplate.html"
controller="AdminController as adminController"
></tab-panel>
or like this
<tab-panel tab-id="tab1"
template="{{model.tabTemplate}}"
controller="{{model.tabController}}"
></tab-panel>
I'm using an ng-if inside each panel to switch the content on and off and an ng-include and ng-controller to load the content in. Here is a simplified test case of my directive.
// Tab Panel Directive Controller
.controller('TabPanelCtrl', function(){
// removed for brevity
})
// Tab Panel Directive
.directive('s4pTabPanel', function($interpolate) {
return {
restrict: 'E',
scope: {
id: '@?tabId',
template: '@?',
controller: '@?',
},
controller: 'TabPanelCtrl as tabPanelCtrl',
template: getTemplate
};
function getTemplate(element, attr) {
// removed for brevity
// Panel loads template and controller
if(attr.template && attr.controller){
return '<tab-panel-inner ng-if="loadContent" ng-include="template" onload="onPanelLoaded()" ng-controller="controller"></tab-panel-inner>';
}
// removed for brevity
}
});
So, the template attribute seems to be fine, the expression is evaluated and the resulting string is inserted into the ng-include attribute. However the ng-controller attribute doesn't like it for some reason and I get the following console error...
Error: ng:areq
Bad Argument
Argument 'controller' is not a function, got string
Anybody help fixing this would really be appreciated.
EDIT:
Just to be clear, the controller on the directive is fine, it's the ng-controller="controller" bit in the directive's template which is causing a problem as it's not getting the evaluated result of the '@' binding where the controller name is passed in.
EDIT 2:
I'm pretty sure it has something to do with the first answer here.
AngularJS: dynamically assign controller from ng-repeat
Specifically this bit:
"Your problem is that ng-controller should point to controller itself, not just string with controller's name."
The name of the controller is contained in a scope variable inside the directive but it's a string, not sure how it can be any other way if I want to keep this dynamic.