[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:

var app = 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: ‘vm‘,
    template: ‘<h2>{{vm.message}}</h2>‘
});
app.component(‘detailContentText‘, {
    bindings: {
        message: ‘<‘
    },
    controller: function () {
    },
    controllerAs: ‘vm‘,
    template: ‘<h3>{{vm.message}}</h3>‘
});
app.component(‘detailTitle‘, {
    bindings: {
        message: ‘<‘
    },
    controller: function () {
    },
    controllerAs: ‘vm‘,
    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: ‘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(‘‘)
});
<!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-controller="AppCtrl as main">
<ng-details>
    <detail-title message="main.detail.title"></detail-title>
    <detail-content-text message="main.detail.text"></detail-content-text>
    <detail-content-image message="main.detail.image"></detail-content-image>
</ng-details>
</body>
</html>
时间: 2024-10-29 00:44:41

[AngularJS] Angular 1.5 multiple transclude的相关文章

AngularJS学习---Routing(路由) &amp; Multiple Views(多个视图) step 7

1.切换分支到step7,并启动项目 git checkout step-7 npm start 2.需求: 在步骤7之前,应用只给我们的用户提供了一个简单的界面(一张所有手机的列表),并且所有的模板代码位于index.html文件中.下一步是增加一个能够显示我们列表中每一部手机详细信息的页面.可以先看一下step6和7的代码区别 . 为了增加详细信息视图,我们可以拓展index.html来同时包含两个视图的模板代码,但是这样会很快给我们带来巨大的麻烦.相反,我们要把index.html模板转变

AngularJs Angular数据类型判断

AngularJs Angular数据类型判断 angular.isArray 判断括号内的值是否为数组. 格式:angular.isArray(value); value: 被判断是否为数组的值. --------------------------------------------------------------- angular.isDate 判断括号内的值是否是一个时间. 格式:angular.isDate(value); value:被判断是否为时间的值. -----------

[AngularJS] Angular 1.5 $transclude with name slot

In Angular 1.5, there is no link and component. So use if you transclude, you cannot access the fifth arguement in link function, which is transcludeFn. But in v1.5, you can access $transclude in controller. And what you can do for that is check whet

angular 自定义指令 directive transclude 理解

项目中断断续续的用了下angular,也没狠下心 认真的学习.angular 特别是自定义指令这块 空白. transclude 定义是否将当前元素的内容转移到模板中.看解释有点抽象. 看解释有点抽象Demo: <!DOCTYPE html> <html lang="en" ng-app='myApp'> <head> <meta charset="UTF-8"> <title>Angularjs</

[Angularjs]angular ng-repeat与js特效加载先后导致的问题

写在前面 最近在项目中遇到这样的一个前端的bug,在ng-repeat中绑定的图片,有一个晃动的特效,在手机端浏览的时候,图片有时候会正常展示,有时就展示不出来.当时猜测是因为angularjs与特效的那些代码加载的先后顺序造成的.有了这样的猜测,就有查找解决方案的方向了. 系列文章 [Angularjs]ng-select和ng-options [Angularjs]ng-show和ng-hide [Angularjs]视图和路由(一) [Angularjs]视图和路由(二) [Angular

AngularJs angular.element

当导入jquery时, angular.element查询出来的是个jquery对象,也可以使用jquery的方式来查询 html: var node=document.getElementById("AttendanceManager"); var ele=angular.element(node); console.dir(ele); var ee=angular.element("#AttendanceManager"); console.dir(ee); 如

Ultimate guide to learning AngularJS in one day

What is AngularJS? Angular is a client-side MVC/MVVM framework built in JavaScript, essential for modern single page web applications (and even websites). This post is a full end to end crash course from my experiences, advice and best practices I've

A Step-by-Step Guide to Your First AngularJS App

What is AngularJS? AngularJS is a JavaScript MVC framework developed by Google that lets you build well structured, easily testable, and maintainable front-end applications. And Why Should I Use It? If you haven't tried AngularJS yet, you're missing

Integrating AngularJS with RequireJS

When I first started developing with AngularJS keeping my controllers and directives in separate .js files for better maintainability I ended up including lots of .js files on my pages. This approach has led to some obvious drawbacks. For example, ea