Angular 自定义指令详解
来源:互联网 发布:mac 设置默认输入法 编辑:程序博客网 时间:2024/06/10 14:38
1.命名:
可以使用普通字符串,也可以使用驼峰法来命名一个指令,例如 firstDirective, 但在使用它时需要以 - 分割: first-directive。
2.创建方式:
Angularjs的指令创建有四种形式,比如创建的指令hello:
(1)元素名(E):
<hello></hello>
(2)标签属性(默认)(A):
<div hello></div>
(3)标签样式类的形式(C):
<div class="hello"></div>
(4)注释(M)(注释号注意有个空格):
<!-- directive: hello -->
3.指令的创建JS代码:myApp.directive('hello',function(){ return { restrict:'AE', template:'<div>hello everyone!</div>', //transclude:false replace:false } });
a.restrict 值可以是以下几种:
E
作为元素名使用,A
作为属性使用,C
作为类名使用,M
作为注释使用
restrict 默认值为 EA
, 即可以通过元素名和属性名来调用指令。
b.replace 解析
初始 html:
E: <hello><div>angular指令学习</div></hello>
如果replace设置为false,最终页面展现的html结果如下:
E: <hello><div>hello everyone!</div></hello>如果replace设置为true,原有 dom 节点被覆盖,最终页面展现的html结果如下:
E:<div>hello everyone!</div>
c.如果想把html 里原有的DOM 结构保留,要设置transclude的值,并利用 ng-transclude 保存原有 html:
myApp.directive('hello',function(){ return { restrict:'ACEM', template:'<div>hello everyone!<div ng-transclude></div></div>', //通过ng-transclude,将默认的 HTML 保存在div标签里 transclude:true } });
HTML 如下显示:(原有的 html 得以保留)
E:<hello><div>hello everyone!<div ng-transclude><div class="ng-scope">angular 指令学习</div></div></hello>
transclude这个配置很重要,可以实现指令的互相嵌套。
参考:①:http://stackoverflow.com/questions/15285635/how-to-use-replace-of-directive-definition
②: http://www.runoob.com/angularjs/angularjs-directives.html
0 0
- Angular 自定义指令详解
- angular自定义指令详解
- angular 自定义指令参数详解
- angular 自定义指令详解 Directive
- angular 自定义指令 Directive(详解)
- angular 自定义指令详解 Directive
- angular 自定义指令详解 Directive
- angular 中的自定义指令之详解API
- angular 自定义指令
- angular js自定义指令
- Angular自定义指令(进阶)
- angular自定义指令
- 创建angular自定义指令
- Angular --- 自定义指令更新
- angular 自定义指令
- angular自定义指令templateUrl
- 一,Angular自定义指令
- Angular自定义指令
- 基本数据类型的字节大小
- 06-图3 六度空间 (30分)
- myeclipse的sping功能添加jdbc功能
- 利用R语言实现spark大数据分析与可视化
- Real-Time Rendering-附录A 线性代数
- Angular 自定义指令详解
- 《C++primer》笔记 第二章2.4const
- Hello world
- CocosCreator学习4:Android平台打包
- (学习总结)鸟哥基础篇第三版:第二十四章
- 水平居中
- 计算机程序的思维逻辑 (67)
- apache,cgi,libcurl和json的初次使用
- 迷失の搜索树(二叉树)