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 ...
随机推荐
- iOS.GetCurrentTimestamp
Cocoa 中测量时间的方法 1. The Methods to Get Current Timestamp iOS中获取时间戳的方法: A. CACurrentMediaTime() B. gett ...
- POJ3662或洛谷1948 Telephone Lines
二分答案+单源最短路 POJ原题链接 洛谷原题链接 显然可以二分答案,检验\(mid\)可以使用最短路来解决. 将大于\(mid\)的边看成长度为\(1\)的边,说明要使用免费升级服务,否则长度为\( ...
- ubuntu12.04下安装Apache+PHP+MySQL
一.Apache1.安装apache2: sudo apt-get install apache2 2.重启apache2: sudo /etc/init.d/apache2 restart 3.在浏 ...
- python 找出一篇文章中出现次数最多的10个单词
#!/usr/bin/python #Filename: readlinepy.py import sys,re urldir=r"C:\python27\a.txt" disto ...
- Django 访问admin提示ViewDoesNotExist at /admin/
ViewDoesNotExist at /admin/ Could not import django.views.generic.simple.redirect_to. Parent module ...
- magento中根据用户的id获取用户的所有订单以及每个订单中的物品 以及物品的相关属性
本篇文章是对于已经有了magento基础的人而言,在某个模块的额controller中写任意一个函数. public function goodbyeAction() { for ($customer ...
- rsync (转载)
rsync 编辑 rsync是类unix系统下的数据镜像备份工具——remote sync. 目录 1简介 2特性 3操作流程 ▪ 服务器端启动 ▪ 客户端同步 4安装 1简介编辑 rsy ...
- java ajax请求后台并获取到返回值
js: $.ajax({ url : '../Islogin.do', type : 'get', cache : false, dataType : 'json', success : functi ...
- sql心跳
因为interactive_timeout决定的是交互连接的时间长短,而wait_timeout决定的是非交互连接的时间长短. 问:为什么需要设置interactive_timeout wait_t ...
- web前端面试题库
web前端面试题及答案 1.常用那几种浏览器测试?有哪些内核(Layout Engine)? 答: (Q1) 浏览器:IE,Chrome,FireFox,Safari,Opera. (Q2) ...