AngularJS transclude 理解及例子
一、概念理解
transclude可以在指令中让使用者自定义模板,也就是说,指令中模板的一部分,让指令的使用者动态指定;与指定中的Scope属性值为{}时候的作用类似,scope属性让指令使用者动态制定属性数据和事件,但是这里是模板,使用scope属性不合适。
也就是说,如果你在定义指令的时候,想要它在具体使用时中间加一些内容,那么你就要用transclusion。
使用时需要通过以下两点:
1、在指令的template中通过属性或者元素的方式标记放置动态内容的位置,比如<ng-transclude></ng-transclude>;ng-tranclude决定了在什么地方放置嵌入部分。
2、在指令的返回对象中增加transclude: true
也就是说,取出自定义指令中的内容(就是写在指令里面的子元素),以正确的作用域解析它,然后再放回指令模板中标记的位置(通常是ng-transclude标记的地方)。
二、transclude与作用域
在定义一个指令时,如果不显式声明scope,那么指令的作用域就是父作用域。如果声明scope:true或者scope:{},那么指令会生成一个自己的作用域,前者是原型继承,后者是独立作用域。使用transclusion,会生成一个新的作用域,直接原型继承于父作用域。
三、例子
1、入门例子
<body ng-app="myApp">
<div class="a">
<my-transclusion name="huyx">嵌入的内容</my-transclusion>
</div>
</body>
angular.module('myApp', []).directive('myTransclusion', function () {
return {
restrict: 'E',
transclude: true,
scope: { name:'@' },
template: '<div>' +
'<div>{{name}}</div><br>' +
'<div ng-transclude></div>' +
'</div>'
};
});
运行结果:

即,把使用指令的地方的内容,这里是“”嵌入的内容“,放到了指令模板中ng-transclude位置。
生成页面源码:

2、复杂点例子
button-bar指令使用的地方的内容(即下边的两个button),替换到指令模板ng-transclusion的地方。而且,在项目不同的地方多出使用该指令,根据需要可以指定不同的动态内容,插入指定的模板指定位置,而指定的其他部分可以共用。
<div ng-controller="parentController">
<button-bar>
<button class="primary" ng-click="onPrimary1Click()">{{primary1Label}}</button>
<button class="primary">Primary2</button>
</button-bar>
</div>
var testapp = angular.module('testapp', []);
testapp.controller('parentController', ['$scope', '$window', function($scope, $window) {
console.log('parentController scope id = ', $scope.$id);
$scope.primary1Label = 'Prime1';
$scope.onPrimary1Click = function() {
$window.alert('Primary1 clicked');
};
}]);
testapp.directive('primary', function() {
return {
restrict: 'C',
link: function(scope, element, attrs) {
element.addClass('btn btn-primary');
}
}
});
testapp.directive('buttonBar', function() {
return {
restrict: 'EA',
template: '<div class="span4 well clearfix"><div class="pull-right" ng-transclude></div></div>',
replace: true,
transclude: true
};
});
AngularJS transclude 理解及例子的更多相关文章
- AngularJS 深入理解 $scope 转载▼
AngularJS 深入理解 $scope 转载▼ (2015-04-07 14:09:50) $scope 的使用贯穿整个 AngularJS App 应用,它与数据模型相关联,同时也是表达 ...
- angularJS transclude
参考来源:彻底弄懂AngularJS中的transclusion 对以上文章进行摘录.总结和测试记录 在使用指令的时候,如果想要使用指令中的子元素,那么你就要用transclusion. 指令的DDO ...
- angular 自定义指令 directive transclude 理解
项目中断断续续的用了下angular,也没狠下心 认真的学习.angular 特别是自定义指令这块 空白. transclude 定义是否将当前元素的内容转移到模板中.看解释有点抽象. 看解释有点抽象 ...
- AngularJS 深入理解 $scope
$scope 的使用贯穿整个 AngularJS App 应用,它与数据模型相关联,同时也是表达式执行的上下文.有了$scope 就在视图和控制器之间建立了一个通道,基于作用域视图在修改数据时会立刻更 ...
- 『AngularJS』理解$Scope
理解$Scope 执行概要 在AngularJS,一个子scope通常原型继承于它的父scope.应用于这个规则的表达式是一个使用scope:{...}的指令,这将创建一个『孤岛』scope(非原型继 ...
- angularjs transclude demo
<!doctype html> <html lang="en" ng-app="expanderModule"> <head> ...
- [AngularJS] Transclude -- using what existing in DOM to replace the template elements in directive
var app = angular.module("phoneApp", []); app.controller("AppCtrl", function($sc ...
- angularjs深入理解向指令传递数据,双向绑定机制
<!DOCTYPE html> <html lang="zh-CN" ng-app="app"> <head> <me ...
- Java ClassLoader加载机制理解 实际例子
针对 Java ClassLoader加载机制理解, 做了个如何自定制简单的ClassLoader,并成功加载指定的类. 不废话,直接上代码. package com.chq.study.cl; im ...
随机推荐
- Github远程仓库关联
一.Git的安装 1.git的安装和配置 (1)配置用户名和邮箱,如下所示: $ git config --global user.name [username] $ git config --glo ...
- HDOJ4261 Estimation
一道需要用堆初始化的\(DP\) 原题链接 显然对于每一个部分,当\(b[i]\)为\(a\)对于部分的中位数时,差错最小.设\(S(x,y)\)表示\(x\sim y\)这一部分的差错. \(DP\ ...
- 从1~N中任选出三个数,最小公倍数最大
已知一个正整数N,问从1~N中任选出三个数,它们的最小公倍数最大可以为多少. 当n为奇数:n.n-1.n-2这是三个最大数,并且它们两两互质.因为连续的奇.偶.奇,互质.连续的两个数互质是因为它们的公 ...
- CSharp陷阱1
CSharp陷阱1 环境:sharpdevelop 3 .net 2.0 正确的 internal static readonly string[] string_2 = new ...
- Delphi中静态方法重载还是覆盖的讨论
Delphi中静态方法重载还是覆盖的讨论 新人学习Delphi的时候,容易搞不懂的一个问题,当子类方法和基类方法同名,并且参数也一样的时候,叫做什么呢?是覆盖,还是重载呢? 答案是隐藏父类方法. 一般 ...
- Minimum Window Substring LT76
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- 上传文件 input file
//-----前端文件------- form id="uploadForm" enctype="multipart/form-data"> <in ...
- Java在dos界面运行java源文件编译成功,但运行虚拟机时出现错误:“找不到或无法加载主类”的问题
(一)首先检查环境变量配置有没有问题, 1PATH为%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin; 2CLASSSPATH为.;%JAVA_HOME%\lib\dt.jar; ...
- 【WebService】WebService之CXF和Spring整合(六)
前面介绍了WebService与CXF的使用,项目中我们经常用到Spring,这里介绍CXF与Spring整合 步骤 1.创建一个Maven Web项目,可以参照:[Maven]Eclipse 使用M ...
- DevOps:从理念到实施
转载自:http://os.51cto.com/art/201404/436794.htm DevOps这个新理念的出现,是为了应对IT环境中普遍面临的一些挑战.开发团队要求的不断满足新的客户需求,并 ...