I'm trying to add a body directive which will have a left panel and a right panel. But in between these panels I will have some content that is private to the body directive. I'm currently using transclude=true option to load the content. But, I'm looking for a way to use two ng-transclude. I investigated a lot how to solve that, but I was not able to find an elegant solution. I had to manually add the transcluded objects in the compile step of the body directive. Following is the way I solved that:
Body Directive
var myApp = angular.module('myApp', []);
myApp.directive('body', function () {
return {
restrict: 'A',
transclude: true,
scope: {
title: '@'
},
templateUrl: 'body.html',
compile: function compile(element, attrs, transclude) {
return function (scope) {
transclude(scope.$parent, function (clone) {
for (var i = 0; i < clone.length; i++){
var el = $(clone[i]);
if(el.attr('panel-left') !== undefined) {
element.find('#firstPanel').html(el);
} else if(el.attr('panel-right') !== undefined) {
element.find('#secondPanel').html(el);
}
}
});
}
}
};
});
myApp.directive('panelLeft', function () {
return {
require: "^body",
restrict: 'A',
transclude: true,
replace: true,
template: '<div ng-transclude></div>'
};
});
myApp.directive('panelRight', function () {
return {
require: "^body",
restrict: 'A',
transclude: true,
replace: true,
template: '<div ng-transclude></div>'
};
});
Template
<script type="text/ng-template" id="body.html">
<h1> {{title}} </h1>
<hr/>
<div id="firstPanel" class="inner-panel"></div>
<div id="innerMiddleContent" class="inner-panel">middle private content here</div>
<div id="secondPanel" class="inner-panel"></div>
</script>
<div body title="Sample Body Directive">
<div panel-left>
Public content that goes on the left
</div>
<div panel-right>
Public content that goes on the right
</div>
</div>
Here is the JSFiddle for this example. I'm looking for something like this:
Good-to-have Template
<script type="text/ng-template" id="body.html">
<h1> {{title}} </h1>
<hr/>
<div id="firstPanel" class="inner-panel" ng-transclude="panel-left"></div>
<div id="innerMiddleContent" class="inner-panel">middle private content here</div>
<div id="secondPanel" class="inner-panel" ng-transclude="panel-right"></div>
</script>
Question: Am I doing something wrong? Is there a recommended way to solve this issue?
ng-transclude
doesn't support splitting transcluded elements to multiple places. BTW, thetransclude
function incompile:
has been deprecated. You should use thetransclude
function inlink:
instead. – Characharabancng-transclude
to use in your project first. – Characharabanc