[AngularJS] Angular 1.5 multiple transclude

If you know ui-router, multi-transclude should be easy for you also. In previou Angular version <1.4, one diretive can only have one transclude element. But now in Angular 1.5, you can give each transclude element a name, then you can have multi-transcluded elements.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="node_modules/angular/angular.min.js"></script>
    <script src="app.js"></script>
</head>
<body ng-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: 'vm',
        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 html code lot more easy to follow.

 

Another benefit is we can choose which element to be 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 is not yet showing on the page.

var app = 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: 'vm',
        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:

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值