If you know Ui-router, Multi-transclude should is easy for you also. In Previou Angular version <1.4, one diretive can only has one transclude element. But under Angular 1.5, you can give each transclude element a name and then you can have multi-transcluded elements.
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title>Title</title> <Scriptsrc= "Node_modules/angular/angular.min.js"></Script> <Scriptsrc= "App.js"></Script></Head><BodyNg-app= "App"><ng-details> <Detail-title>Details</Detail-title> <Detail-content-text>This is text content</Detail-content-text></ng-details></Body></HTML>
var app = Angular.module (' app ', []); App.directive (' Ngdetails ', function () {return{restrict:E, scope: {}, transclude: {‘title‘: ‘Detailtitle‘,//title:used in Directive template, detailtitle:used in app HTML‘textcontent‘: ‘Detailcontenttext‘}, Controller:function () { This. Toggle =true; This. Toggleit =function () { This. Toggle =! This. Toggle; }}, Controlleras:' VMS ', Template: [' <div class= ' details > ', ' <div class= ' summary ' ng-click= ' open =!open ' > ', ' {{open? \ ' ▾\ ': \ ' ▸\ '} ', ' <span ng-transclude= 'title">default title</span> ', ' </div> ', ' <div class= ' content "ng-if=" Vm.toggle "ng-show=" open "ng-transclude="textcontent">default text</div> ', ' </div> '].join (‘‘) };});
This can make the HTML code lot and easy to follow.
Another benefit is the we can choose which element to being transcluded into our template:
<ng-details> <Detail-title>Details</Detail-title> <Detail-content-text>This is text content</Detail-content-text> <Detail-content-image>Sorry there is no image</Detail-content-image></ng-details>
In app HTML, we add one more transclude element:detail-content-image, but it's not yet showing on the page.
varApp = Angular.module (' app ', []); App.directive (' Ngdetails ',function () { return{restrict:E, scope: {}, transclude: {' title ': ' Detailtitle ', ' Textcontent ': ' Detailcontenttext ', ' imagecontent ': ' Detailcontentimage ' , }, Controller:function(){ This. Toggle =true; This. Toggleit =function(){ This. Toggle =! This. Toggle; }}, Controlleras:' VMS ', Template: [' <div class= ' details > ', ' <div class= ' summary ' ng-click= ' open =!open ' > ', ' {{open? \ ' ▾\ ': \ ' ▸\ '} ', ' <span ng-transclude= ' title ' >default title</span> ', ' </div> ', ' <div class= ' content "ng-if=" Vm.toggle "ng-show=" open "ng-transclude=" textcontent ">default text</div> ', ' <div class= ' content "ng-if="!vm.toggle "ng-show=" open "ng-transclude=" imagecontent ">default image</ Div> ', ' </div> ', ' <button ng-click= ' Vm.toggleit () ">toggle it: {{vm.toggle}}</button> '].join (‘‘) };});
So, based on the toggle button, the template can choose which element to transclude into the template.
---------------------------------
Component Refactor:
varApp = Angular.module (' app ', []);
App.controller (' Appctrl ',function() { This. Detail ={title:"Detail title", Text:"Text Content", Image:"Image Content" };});
App.component (' Detailcontentimage ', {bindings: {message:' < '//One time binding}, Controller:function() {}, Controlleras:' VMS ', Template:' });
App.component (' Detailcontenttext ', {bindings: {message:' < '}, Controller:function() {}, Controlleras:' VMS ', Template:' });
App.component (' Detailtitle ', {bindings: {message:' < '}, Controller:function() {}, Controlleras:' VMS ', Template:' <span>{{vm.message}}</span> '});
App.component (' Ngdetails ', {bindings: {data:' < '}, transclude: {' title ': ' Detailtitle ', ' Textcontent ': ' Detailcontenttext ', ' Imagecontent ': ' Detailcontentimage '}, Controller:function () { This. Toggle =true; This. Toggleit =function () { This. Toggle =! This. Toggle; }}, Controlleras:' VMS ', Template: [' <div class= ' details > ', ' <div class= ' summary ' ng-click= ' open =!open ' > ', ' {{open? \ ' ▾\ ': \ ' ▸\ '} ', ' <span ng-transclude= ' title ' >default title</span> ', ' </div> ', ' <div class= ' content "ng-if=" Vm.toggle "ng-show=" open "ng-transclude=" textcontent ">default text</div> ', ' <div class= ' content "ng-if="!vm.toggle "ng-show=" open "ng-transclude=" imagecontent ">default image</div > ', ' </div> ', ' <button ng-click= ' Vm.toggleit () ">toggle it: {{vm.toggle}}</button> '].join (‘‘)});
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title>Title</title> <Scriptsrc= "Node_modules/angular/angular.min.js"></Script> <Scriptsrc= "App.js"></Script></Head><BodyNg-app= "App"Ng-controller= "Appctrl as main"><ng-details> <Detail-titlemessage= "Main.detail.title"></Detail-title> <Detail-content-textmessage= "Main.detail.text"></Detail-content-text> <Detail-content-imagemessage= "Main.detail.image"></Detail-content-image></ng-details></Body></HTML>
[AngularJS] Angular 1.5 Multiple Transclude