[AngularJS] Directive with Transcluded Elements

时间:2022-09-17 09:59:30

Create a wrapWith directive using advanced transclusion techniques.

transclude - compile the content of the element and make it available to the directive. Typically used with ngTransclude. The advantage of transclusion is that the linking function receives a transclusion function which is pre-bound to the correct scope. In a typical setup the widget creates an isolate scope, but the transclusion is not a child, but a sibling of the isolate scope. This makes it possible for the widget to have private state, and the transclusion to be bound to the parent (pre-isolate) scope.

true - transclude the content of the directive. (replace the whole stuff)

'element' - transclude the whole element including any directives defined at lower priority. (append to the dom)

See more: http://www.cnblogs.com/Answer1215/p/3932450.html

<!DOCTYPE html>
<html>
<head>
<title>Egghead.io</title>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css"/>
</head>
<body ng-app="egghead" ng-controller="AppCtrl as app"> <cg-form wrap-with="well"></cg-form> <!-- get all the template from directive put into well template -->
<cg-form wrap-with="red"></cg-form> <!-- get all the template from directive put into red template -->
 <script id="form-info" type="text/ng-template"> <form role="form"> <div class="form-group"> <label for="exampleInputEmail1">Email address</label> <input type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email"> </div> <div class="form-group"> <label for="exampleInputPassword1">Password</label> <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password"> </div> <button type="submit" class="btn btn-default">Submit</button> </form> </script> <script id="well" type="text/ng-template"> <div class="well"></div> </script> <script id="red" type="text/ng-template"> <div style="color: red"></div> </script> <script src="bower_components/angular/angular.min.js"></script> <script src="app.js"></script> </body> </html>
var egghead = angular.module("egghead", []);

egghead.controller("AppCtrl", function () {
var app = this; app.people = [{"firstName":"Sonia","lastName":"Hodges"},{"firstName":"Benedict","lastName":"Morrow"},{"firstName":"Keegan","lastName":"Fields"},{"firstName":"Jade","lastName":"Martinez"},{"firstName":"Jaquelyn","lastName":"Suarez"},{"firstName":"Leo","lastName":"Hughes"},{"firstName":"Skyler","lastName":"Sharp"},{"firstName":"Genevieve","lastName":"Villarreal"},{"firstName":"Beau","lastName":"Hendrix"},{"firstName":"Lara","lastName":"Howard"},{"firstName":"Jonah","lastName":"Crawford"},{"firstName":"Kendall","lastName":"Lane"},{"firstName":"Kimberly","lastName":"Mcclain"},{"firstName":"Ingrid","lastName":"Salinas"},{"firstName":"Moses","lastName":"Mcpherson"},{"firstName":"Britanney","lastName":"Sweeney"},{"firstName":"Patricia","lastName":"Perez"},{"firstName":"Roth","lastName":"Heath"},{"firstName":"Nora","lastName":"Osborne"},{"firstName":"Giacomo","lastName":"Shepard"}]
}); egghead.directive("wrapWith", function ($templateCache) {
return {
transclude: 'element',
link: function (scope, element, attrs, ctrl, transclude) {
var template = $templateCache.get(attrs.wrapWith);
console.log(template); var templateElement = angular.element(template); console.log(element); transclude(scope, function (clone) {
element.after(templateElement.append(clone));
})
}
}
}); egghead.directive("cgForm", function ($templateCache) {
return {
restrict: "EA",
templateUrl: "form-info"
}
});

result:

[AngularJS] Directive with Transcluded Elements

[AngularJS] Directive with Transcluded Elements的更多相关文章

  1. 学习AngularJs&colon;Directive指令用法(完整版)

    这篇文章主要学习AngularJs:Directive指令用法,内容很全面,感兴趣的小伙伴们可以参考一下   本教程使用AngularJs版本:1.5.3 AngularJs GitHub: http ...

  2. 学习AngularJs&colon;Directive指令用法

    跟我学AngularJs:Directive指令用法解读(上) http://blog.csdn.net/evankaka/article/details/51232895 跟我学AngularJs: ...

  3. angularjs directive 实例 详解

    前面提到了angularjs的factory,service,provider,这个可以理解成php的model,这种model是不带html的,今天所说的directive,也可以理解成php的mo ...

  4. Angularjs directive全面解读(1&period;4&period;5)

    说到Angularjs directive即指令,可以这么说Angularjs的灵魂就是指令,学会Angularjs指令那么你的Angularjs的武功就修炼了一半了,当然这只是鄙人的一点点独到见解, ...

  5. angularjs directive &lpar;自定义标签解析&rpar;

    angularjs directive (自定义标签解析) 定义tpl <!-- 注意要有根标签 --> <div class="list list-inset" ...

  6. angularJS directive详解

    前言 最近学习了下angularjs指令的相关知识,也参考了前人的一些文章,在此总结下. 欢迎批评指出错误的地方. Angularjs指令定义的API AngularJs的指令定义大致如下 angul ...

  7. AngularJs directive &&num;39&semi;transclude&&num;39&semi; option 详解

    transclude好像不是一个英语单词,有道词典里没有,百度翻译的意思是嵌入. transclude在angularjs的自定义的derective中是比较常见的一个东西,所有有必要要了解它. 我们 ...

  8. angularJS directive详解(自定义指令)

    Angularjs指令定义的API AngularJs的指令定义大致如下 其中return返回的对象包含很多参数,下面一一说明 1.restrict (字符串)可选参数,指明指令在DOM里面以什么形式 ...

  9. 50&period;AngularJs directive详解及示例代码

    转自:https://www.cnblogs.com/best/tag/Angular/ 本教程使用AngularJs版本:1.5.3 AngularJs GitHub: https://github ...

随机推荐

  1. avalon复杂绑定

    样式操作:ms-css-样式名=“样式值”,ms-class ms-css-width="prop"(自动补px) ms-css-height="{{prop}}%&qu ...

  2. JAVA开发相关

    JAVA开发相关1. IntelliJ IDEA开发工具熟练使用2. Maven3. Spring框架(IoC.AOP) 1)数据库相关MyBatis 2)数据库连接池 3)事务.多数据源.跨数据库分 ...

  3. Gulp&period;js 参考手册,自动化构建利器

    Gulp 是最新的基于 Node 的自动化构建工具,希望能够取代 Grunt,成为最流行的 JavaScript 任务运行器.通过结合 NodeJS 的数据流的能力,只需几步就能搭建起自己的自动化项目 ...

  4. ecshop 配送方式支持&quot&semi;货到付款&quot&semi;

    ecshop配送方式,支持货到付款方法. 打开表 ecs_shipping,找到字段support_cod,修改1则支持货到付款 扩展 问题描述:已经修改了ecs_shipping表里的support ...

  5. Vcenter server 5&period;5上传ISO镜像

    1.摘要 --- 浏览要上传的硬盘. 2.创建一个文件夹专业门存放ISO镜像. 3.选择上传文件. 4.选择文件上传即可.

  6. Delphi下使用OpenOffice&plus;JodConverter&plus;SWFtools进行文件转换

    目的:office文件转换为PDF或SWF,最终可使用Flexpaper调用adobe flash player进行浏览 放弃两个文件转换工具: 1.FlashPaper,转换出的文件由于自带工具栏, ...

  7. cocos2dx android版本移植时的Error format not a string literal and no format arguments解决方案

    原文地址 : http://www.cnblogs.com/hhuang2012/p/3336911.html cocos2dx android版本移植时的Error format not a str ...

  8. 为什么很多应用都安装在&sol;usr&sol;local目录下

    为什么很多应用都安装在/usr/local目录下   很多应用都安装在/usr/local下面,那么,这些应用为什么选择这个目录呢?理解了最根源的原因后,也许对你理解linux组织文件的方式有更直观的 ...

  9. spring mvc 拦截器

    拦截器作用:可以用于用户操作的安全检查,如:登录.权限等 package com.tool; import java.util.List; import javax.servlet.http.Http ...

  10. Windows命令行使用FTP

    1.系统环境 FTP客户端:Windows7旗舰版,管理员权限命令行: FTP服务端:CentOS 6.5,VSFTP,端口 21(默认) 2.登陆FTP 在命令行下输入 ftp,出现 ftp> ...