我正在研究一个自定义指令,我将在单击选项卡时从控制器获取模板以进行编译。以下是我的模板、自定义指令和 html 代码。
控制器 :
var template = '';
$scope.onTabClick = function(eachTab) {
if (eachTab.id == 1) {
template = ' <div class="innerChartsDiv"> <div class="col"> <p class="graphtitle"> {{title1Text}}</p> <c3-simple id="dashboard1Data" config="dashboard1Data"></c3-simple> </div> <div class="col"> <p class="graphtitle"> {{title2Text}}</p> <c3-simple id="dashboard2Data" config="dashboard2Data"></c3-simple> </div> </div> ';
}
if (eachTab.id == 2) {
template = ' <div class="innerChartsDiv"> <div class="col"> <p class="graphtitle"> {{title6Text}}</p> <c3-simple id="dashboard6Data" config="dashboard6Data"></c3-simple> </div> <div class="col"> <p class="graphtitle"> {{title10Text}}</p> <c3-simple id="dashboard10Data" config="dashboard10Data"></c3-simple> </div> </div> ';
}
if (eachTab.id == 3) {
template = ' <div class="innerChartsDiv"> <div class="col"> <p class="graphtitle"> {{title16Text}}</p> <c3-simple id="dashboard16Data" config="dashboard16Data"></c3-simple> </div> <div class="col"> <p class="graphtitle"> {{title4Text}}</p> <c3-simple id="dashboard4Data" config="dashboard4Data"></c3-simple> </div> </div> ';
}
}
我的自定义指令:
app.directive('customCharts', ['$compile', function($compile) {
return {
restrict: 'EA',
scope: {
dashboard1Data: '=',
title1Text: '=',
dashboard2Data: '=',
title2Text: '=',
template: '='
},
link: function(scope, element, attrs) {
scope.$watch('templatedyn', function() {
var parent = angular.element(document.querySelectorAll('.customChartsDiv')) // DOM element where the compiled template can be appended
var linkFn = $compile(scope.template);
var content = linkFn(scope);
parent.append(content);
});
}
}
}]);
在jsp页面中我有:
<div class="customChartsDiv">
<div custom-charts dashboard1-data="dashboard1Data" title1-text="title1Text" dashboard2-data="dashboard2Data" title2-text="title2Text" template="template"></div>
</div>
如果满足条件,我的自定义指令工作正常并在$scope.onTabClick
功能中正确显示图表。eachTab.id == 1
我怎样才能使指令通用,以便即使满足eachTab.id == 2
oreachTab.id == 3
条件它也能工作?