[AngularJS] Angular 1.5 Multiple Transclude

Source: Internet
Author: User

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? \ ' &blacktriangledown;\ ': \ ' &blacktriangleright;\ '} ',                    ' <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? \ ' &blacktriangledown;\ ': \ ' &blacktriangleright;\ '} ',                    ' <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? \ ' &blacktriangledown;\ ': \ ' &blacktriangleright;\ '} ', ' <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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.