angularjs指令系统系列课程(3):替换replace,内容保留transclude,作用方式restrict
这一节我们主要看一下replace,transclude,restrict这三个参数
1.replace
可取值:bool
默认为:true
对于replace属性,设置为false表示原有指令标识不会被替换,设置为true的时候 原有指令被替换,咱们看一个例子:
js
newsApp.directive('helloDirective', function() {
return {
template: '<div>hello directive</div>',
replace: true
}
});
html:
<section>
<div hello-directive> </div>
</section>
执行后,发现原有指令标识被替换。
2.transclude
可取值:bool
默认为false,
当transclude为true时,指令作用的元素内部的内容就可以不被清除掉,但是只用transclude是不够的,我们需要配合template使用:看一个例子
js
newsApp.directive('helloDirective', function() {
return {
template: '<div><span>hello directive</span><div ng-transclude></div></div>',
transclude: true
}
});
html
<section>
<div hello-directive>
<div>我是原内容</div>
</div>
</section>
执行后结果:
审查源代码=》:
可见,原内容被保留了下来,并且被装进了我们在template里面定义的一个ng-transclude的容器里面
3.restrict
可取值:E,A,C,M
默认值为:A
该参数的作用在于,指令在以什么样的方式作用dom元素,可以组合使用,比如:restrict:'EAC',这样就支持了三种引入方式
E:element(元素)
A:attribute(属性)
C:class(类)
M:注释
比如指令:helloDirective
我可以用这么几种方式展示:
第一种:以属性(A)方式作用于dom元素,默认为A,所以可以不写
js
newsApp.directive('helloDirective', function() {
return {
template: '<div><span>hello directive</span><div ng-transclude></div></div>',
transclude: true,
restrict:'A'
}
});
HTML
<section>
<div hello-directive>
<div>我是原内容</div>
</div>
</section>
第二种:以元素(E)方式作用于dom元素
js
newsApp.directive('helloDirective', function() {
return {
template: '<div><span>hello directive</span><div ng-transclude></div></div>',
transclude: true,
restrict:'E'
}
});
html:
<section>
<hello-world>
<div>我是原内容</div>
</hello-world>
</section>
第三种:以类(C)方式作用于dom元素
js
newsApp.directive('helloDirective', function() {
return {
template: '<div><span>hello directive</span><div ng-transclude></div></div>',
transclude: true,
restrict:'C'
}
});
html:
<section>
<div class="{{hello-directive}}">
<div>我是原内容</div>
</div>
</section>
第四种:注释(M)的方式作用于dom元素
js
newsApp.directive('helloDirective', function() {
return {
template: '<div><span>hello directive</span><div ng-transclude></div></div>',
transclude: true,
restrict:'M',
link:function(){
alert("Has");
}
}
});
html
<section>
<div>我是原有内容</div>
<!--directive:hello-directive-->
</section>
结果输出后发现,html里面应该显示的hello directive没变,
但是 link函数起作用了,如约弹出来了“Has",
这是因为 注释的方式引入的指令时不会修改dom元素的,只会作用于compile和link函数
angularjs指令系统系列课程(3):替换replace,内容保留transclude,作用方式restrict的更多相关文章
- angularjs指令系统系列课程(1):目录
angularjs里面有一套十分强大的指令系统 比如内置指令:ng-app,ng-model,ng-repeat,ng-init,ng-bind等等 从现在开始我们讲解AngularJS自定义指令, ...
- angularjs指令系统系列课程(5):控制器controller
这一节我们来说一下controller这个参数 一.指令控制器的配置方法: 对于指令我有两种配置路由的方式:1.在html中直接引用,2,在指令的controller参数里定义 第一种:在html中直 ...
- angularjs指令系统系列课程(2):优先级priority,模板template,模板页templateUrl
今天我们先对 priority,template,templateUrl进行学习 1.priority 可取值:int 作用:优先级 一般priority默认为0,数值越大,优先级越高.当一个dom元 ...
- angularjs指令系统系列课程(4):作用域Scope
指令的scope对象是一个很重要,很复杂的对象,我们这一节作为重点讲解 可取值: 1.false(默认), 2.true, 3.{}(object) 1.false:默认值,不创建新的作用域 2.tr ...
- ASP.NET MVC框架开发系列课程 (webcast视频下载)
课程讲师: 赵劼 MSDN特邀讲师 赵劼(网名“老赵”.英文名“Jeffrey Zhao”,技术博客为http://jeffreyzhao.cnblogs.com),微软最有价值专家(ASP.NET ...
- angularjs自动化测试系列之jasmine
angularjs自动化测试系列之jasmine jasmine参考 html <!DOCTYPE html> <html lang="en"> <h ...
- handlebars.js 用 <br>替换掉 内容的换行符
handlebars.js 用 <br>替换掉 内容的换行符 JS: Handlebars.registerHelper('breaklines', function(text) { te ...
- solr与.net系列课程(九)solr5.1的配置
solr与.net系列课程(九)solr5.1的配置 最近一些园友来咨询solr5.1的配置方式,然后我就去官网下载了个最新版本的solr,发现solr5.0以后solr的下载包里的内容发生的变化,移 ...
- solr与.net系列课程(五)solrnet的使用
solr与.net系列课程(五)solrnet的使用 最近因项目比较忙,所以这篇文章出的比较晚,离上一篇文章已经有半个月的时间了,这节课我们来学下一下solr的.net客户端solrnet 出处 ...
随机推荐
- [原创]java WEB学习笔记106:Spring学习---AOP的通知 :前置通知,后置通知,返回通知,异常通知,环绕通知
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- @RequestMapping注解详解
@RequestMapping是一个用来处理请求地址映射的注解,可用于类或者方法上.用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径.@RequestMapping注解有六个属性,下面进行 ...
- 关于DoesObjectExist
目录: 我们可以看到这个文件夹目录是存在的 文件: 然后,我们运行测试一下: 文件存在检测成功(正常) 文件夹存在,但检测失败! ??? 明明存在的,为什么检测不到……
- 空间不支持openssl解决办法
windows server2003 系统 IIS6.0 ,支付宝担保交易功能不能用,提示您的空间不支持openssl 解决办法如下:1.在C:\WINDOWS 下打开php.ini 将 extens ...
- Apache+Tomcat实现负载均衡
反向代理负载均衡 (Apache2+Tomcat7/8) 使用代理服务器可以将请求转发给内部的Web服务器,让代理服务器将请求均匀地转发给多台内部Web服务器之一上,从而达到负载均衡的目的.这种代理方 ...
- SQL Server 参数化 PARAMETERIZATION
ALTER DATABASE dbname SET PARAMETERIZATION SIMPLE --默认 ALTER DATABASE dbname SET PARAMETERIZATI ...
- 像Maven一样构建java项目的目录,更好的管理java工程的源码
都知道maven具有管理Java或者Javaweb的功能.我个人尤其看中的是其代码层次的分离.不同的代码在不同的文件夹下.这是在eclipse新建一个普通的工程无法实现的.而如果用maven实现有时候 ...
- 分布式入门之2:Quorum机制
1. 全写读1(write all, read one) 全写读1是最直观的副本控制规则.写时,只有全部副本写成功,才算是写成功.这样,读取时只需要从其中一个副本上读数据,就能保证正确性. 这种规则 ...
- 用友ERP-U8最新破解(再次更新版本,附安装过程中的解决办法)
新版用友u8.70下载地址:http://ftp.shangyuchem.com/应用软件/用友ERP-U8管理软件(8.70版).rar 准备好安装环境,因为需要SQLSERVER和IIS支持,而个 ...
- 无法打开物理文件xxx.mdf操作系统错误 5:“5(拒绝访问。)” (Microsoft SQL Server,错误: 5120)的解决方法
无法打开物理文件xxx.mdf操作系统错误 5:“5(拒绝访问.)” (Microsoft SQL Server,错误: 5120)的解决方法 问题描述: 在附加数据库到sql server时,附 ...