angular指令的transclude属性是一个让初学者比较难以理解的地方,transclude可以设置为false(默认),true或者对象三种值,如果不设该属性就默认为false,也就是说你不需要将该指令所在元素包含的内容嵌入到模板中。

当transclude为true的时候,这时指令所在元素包含的内容会被嵌入到模板中有ng-transclude指令的元素中,例如:

index.html

<!DOCTYPE html>
<html ng-app="myapp">
<head>
<meta charset="utf-8">
<title>angular test</title>
</head>
<body ng-controller="myCtrl">
<div hello="{{name}}">你好</div>
</body>
<script src="./node_modules/angular/angular.js"></script>
<script src="./index.js"></script>
</html>

index.js

let app = angular.module('myapp',[]);
app.controller('myCtrl', $scope =>{
$scope.name = "Jhon";
});
app.directive('hello', () =>{
return {
restrict: 'A',
template: '<div><span ng-transclude></span>{{name}}</div>',
transclude: true,
scope:{
name: "@hello"
}
}
});

运行之后的效果如下:

<div hello="Jhon" class="ng-isolate-scope">
<div class="ng-binding">
<span ng-transclude="">你好</span>
Jhon
</div>
</div>

当指令元素包含的内容需要嵌入到指令模板不同地方的时候,这个时候就要把transclude设置为对象,例如下面这个我在项目中使用的一个例子:

index.html

<!DOCTYPE html>
<html ng-app="myapp">
<head>
<meta charset="utf-8">
<title>angular test</title>
</head>
<body ng-controller="myCtrl">
<panel>
<panel-header>{{title}}</panel-header>
<panel-body>{{content}}</panel-body>
<panel-footer>{{footer}}</panel-footer>
</panel>
</body>
<script src="./node_modules/angular/angular.js"></script>
<script src="./index.js"></script>
</html>

index.js

let app = angular.module('myapp',[]);
app.controller('myCtrl', ['$scope', $scope =>{
$scope.title = "标题";
$scope.content = "内容";
$scope.footer = "页脚";
}]);
app.directive('panel', () =>{
return {
restrict: 'E',
replace: true,
transclude: {
'header': '?panelHeader',
'body': 'panelBody',
'footer': '?panelFooter'
},
template: `
<div class="panel">
<div class="panel-header" ng-transclude="header"></div>
<div class="panel-body" ng-transclude="body"></div>
<div class="panel-footer" ng-transclude="footer"></div>
</div>`
}
});

显示结果如下:

<div class="panel">
<div class="panel-header" ng-transclude="header">
<panel-header class="ng-binding ng-scope">
标题
</panel-header>
</div>
<div class="panel-body" ng-transclude="body">
<panel-body class="ng-binding ng-scope">
内容
</panel-body>
</div>
<div class="panel-footer" ng-transclude="footer">
<panel-footer class="ng-binding ng-scope">
页脚
</panel-footer>
</div>
</div>

这里指令元素内部有三个指令,这三个指令必须以E的形式调用,它们分别要插入到模板的不同位置,tranclude指定了要插入的位置,transclude是一个键值对的对象,key指定了要插入模板的位置,value就是要插入的内容,?代表这个嵌入点不一定有指令存在,否则必须在这个点插入指令,不然会报错。

值得注意的是,这个实例也证明了一点,指令包含的元素的作用域继承自指令的父作用域而不是隔离作用域。

除了使用ng-transclude指令指定内容嵌入的地方外,我们还有两种方法可以做到这点。

第一种就是在控制器中使用$transclude服务,例如以下代码:

index.html

<!DOCTYPE html>
<html ng-app="myapp">
<head>
<meta charset="utf-8">
<title>angular test</title>
</head>
<body ng-controller="myCtrl">
<hello name="{{name}}"><span>{{action}}</span></hello>
</body>
<script src="./node_modules/angular/angular.js"></script>
<script src="./index.js"></script>
</html>

index.js

let app = angular.module('myapp',[]);
app.controller('myCtrl', ['$scope', $scope =>{
$scope.name = "Jhon";
$scope.action = "你好";
}]);
app.directive('hello', () =>{
return {
restrict: 'E',
transclude: true,
controller: ['$scope', '$element', '$transclude', ($scope, $element, $transclude) =>{
$transclude(clone =>{
//$element.find只能通过标签名进行查找
$element.find('span').append(clone);
});
}],
template: '<div><span></span>{{name}}</div>',
scope: {
name: '@'
}
}
});

最后显示的结果如下:

<hello name="Jhon" class="ng-isolate-scope">
<div class="ng-binding">
<span>
<span class="ng-binding ng-scope">
你好
</span>
</span>
Jhon
</div>
</hello>

其中控制器中的$element就是编译了之后的模板,而$transclude中的参数clone则是被编译了的指令包含的内容。两者可以同时对模板和内容进行处理。

另一种方法是在compile中指定第三个transcludeFn参数,如下所示:

index.js

let app = angular.module('myapp',[]);
app.controller('myCtrl', ['$scope', $scope =>{
$scope.name = "Jhon";
$scope.action = "你好";
}]);
app.directive('hello', () =>{
return {
restrict: 'E',
transclude: true,
compile: (tElement, tAttrs, transcludeFn) =>{
return (scope, element, attrs) =>{
scope.action = "hello";
transcludeFn(scope, (clone) =>{
element.find('span').append(clone);
});
};
},
template: '<div><span></span>{{name}}</div>',
scope: {
name: '@'
}
}
});

这个文件实现了以上控制器中相同的功能,transcludeFn接受两个参数,一个scope作用域,一个函数,和controller一样,这个函数的参数clone就是编译之后的要嵌入的内容。唯一不同的是,编译这个clone的作用域是传进去的第一个参数,而controller中clone是继承了指令的父作用域。

angular高级篇之transclude使用详解的更多相关文章

  1. SaltStack 入门到精通第三篇:Salt-Minion配置文件详解

    SaltStack 入门到精通第三篇:Salt-Minion配置文件详解 作者:ArlenJ  发布日期:2014-06-09 17:52:16   ##### 主要配置设置 ##### 配置 默认值 ...

  2. Farseer.net轻量级开源框架 入门篇:查询数据详解

    导航 目   录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 入门篇: 删除数据详解 下一篇:Farseer.net轻量级开源框架 中级篇: Where条 ...

  3. Mysql高手系列 - 第19篇:mysql游标详解,此技能可用于救火

    Mysql系列的目标是:通过这个系列从入门到全面掌握一个高级开发所需要的全部技能. 这是Mysql系列第19篇. 环境:mysql5.7.25,cmd命令中进行演示. 代码中被[]包含的表示可选,|符 ...

  4. SaltStack 入门到精通第二篇:Salt-master配置文件详解

    SaltStack 入门到精通第二篇:Salt-master配置文件详解     转自(coocla):http://blog.coocla.org/301.html 原本想要重新翻译salt-mas ...

  5. Farseer.net轻量级开源框架 入门篇:添加数据详解

    导航 目   录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 入门篇: 分类逻辑层 下一篇:Farseer.net轻量级开源框架 入门篇: 修改数据详解 ...

  6. Farseer.net轻量级开源框架 入门篇:修改数据详解

    导航 目   录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 入门篇: 添加数据详解 下一篇:Farseer.net轻量级开源框架 入门篇: 删除数据详解 ...

  7. Farseer.net轻量级开源框架 入门篇:删除数据详解

    导航 目   录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 入门篇: 修改数据详解 下一篇:Farseer.net轻量级开源框架 入门篇: 查询数据详解 ...

  8. Mysql高手系列 - 第12篇:子查询详解

    这是Mysql系列第12篇. 环境:mysql5.7.25,cmd命令中进行演示. 本章节非常重要. 子查询 出现在select语句中的select语句,称为子查询或内查询. 外部的select查询语 ...

  9. 《手把手教你》系列基础篇(九十六)-java+ selenium自动化测试-框架之设计篇-跨浏览器(详解教程)

    1.简介 从这一篇开始介绍和分享Java+Selenium+POM的简单自动化测试框架设计.第一个设计点,就是支持跨浏览器测试. 宏哥自己认为的支持跨浏览器测试就是:同一个测试用例,支持用不同浏览器去 ...

随机推荐

  1. 克隆虚拟机 virtualbox 修改 uuid

    cmd E:\Program Files\Oracle\VirtualBox>VBoxManage.exe internalcommands sethduuid "E:\Program ...

  2. All about Div内显示滚动桥

    Div内显示滚动桥看似是一个简单的前端问题,然而实际会发现还是有挺多需要注意的, 本文尝试对div内显示滚动桥的各种主要实现及一些难题进行研究. 横向滚动桥 横向滚动桥比较简单,无需设置宽度,直接ov ...

  3. 腾讯x5Webview取代原生android Webview

    一.官方地址: https://x5.tencent.com/tbs/ 二.不需要申请开发者,QQ直接登录,下载即可集成到项目中. 三.与原生的webview对比优势 1) 速度快:相比系统webvi ...

  4. Kafka快速上手(2017.9官方翻译)

    为了帮助国人更好了解.上手kafka,特意翻译.修改了个文档.官方Wiki : http://kafka.apache.org/quickstart 快速开始 本教程假定您正在开始新鲜,并且没有现有的 ...

  5. 计算机基础--Java中int char byte的关系

    计算机基础--Java中int char byte的关系 重要:一个汉字占用2byte,Java中用char(0-65535 Unicode16)型字符来存字(直接打印输出的话是字而非数字),当然要用 ...

  6. Spring Boot Document Part I

    最近准备学习Spring Boot 随便翻一下官方的文档 Part I. Spring Boot Documentation Spirng Boot简短介绍,作为接下来内容的导航,可快速预览本章内容. ...

  7. 对python编程的初步理解

    一直以来零零散散有听过python,这周终于下定决心学python了.在网上了买个套视频教程,内容分周次学习,有详细的讲解.本人觉得非常好.这里谈谈一下第一周的学习的笔记.望路过的大神给予指正,不胜感 ...

  8. ASP.Net MVC 布局页 模板页 使用方法详细说明

    一.Views文件夹 -> Shared文件夹下的 _Layout.cshtml 母版页 @RenderBody 当创建基于_Layout.cshtml布局页面的视图时,视图的内容会和布局页面合 ...

  9. C#实现断点续传

    断点续传的原理在了解HTTP断点续传的原理之前,先来说说HTTP协议,HTTP协议是一种基于tcp的简单协议,分为请求和回复两种.请求协议是由客户机(浏览器)向服务器(WEB SERVER)提交请求时 ...

  10. 如何用Django从零开始搭建一个网站(0)

    python,django等安装就直接略过了.下面直接奔如主题,搭建网站. Step1:新建一个django project,运行命令:‘django-admin startproject myPit ...