这一节我们主要看一下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的更多相关文章

  1. angularjs指令系统系列课程(1):目录

    angularjs里面有一套十分强大的指令系统 比如内置指令:ng-app,ng-model,ng-repeat,ng-init,ng-bind等等 从现在开始我们讲解AngularJS自定义指令, ...

  2. angularjs指令系统系列课程(5):控制器controller

    这一节我们来说一下controller这个参数 一.指令控制器的配置方法: 对于指令我有两种配置路由的方式:1.在html中直接引用,2,在指令的controller参数里定义 第一种:在html中直 ...

  3. angularjs指令系统系列课程(2):优先级priority,模板template,模板页templateUrl

    今天我们先对 priority,template,templateUrl进行学习 1.priority 可取值:int 作用:优先级 一般priority默认为0,数值越大,优先级越高.当一个dom元 ...

  4. angularjs指令系统系列课程(4):作用域Scope

    指令的scope对象是一个很重要,很复杂的对象,我们这一节作为重点讲解 可取值: 1.false(默认), 2.true, 3.{}(object) 1.false:默认值,不创建新的作用域 2.tr ...

  5. ASP.NET MVC框架开发系列课程 (webcast视频下载)

    课程讲师: 赵劼 MSDN特邀讲师 赵劼(网名“老赵”.英文名“Jeffrey Zhao”,技术博客为http://jeffreyzhao.cnblogs.com),微软最有价值专家(ASP.NET ...

  6. angularjs自动化测试系列之jasmine

    angularjs自动化测试系列之jasmine jasmine参考 html <!DOCTYPE html> <html lang="en"> <h ...

  7. handlebars.js 用 <br>替换掉 内容的换行符

    handlebars.js 用 <br>替换掉 内容的换行符 JS: Handlebars.registerHelper('breaklines', function(text) { te ...

  8. solr与.net系列课程(九)solr5.1的配置

    solr与.net系列课程(九)solr5.1的配置 最近一些园友来咨询solr5.1的配置方式,然后我就去官网下载了个最新版本的solr,发现solr5.0以后solr的下载包里的内容发生的变化,移 ...

  9. solr与.net系列课程(五)solrnet的使用

     solr与.net系列课程(五)solrnet的使用 最近因项目比较忙,所以这篇文章出的比较晚,离上一篇文章已经有半个月的时间了,这节课我们来学下一下solr的.net客户端solrnet 出处   ...

随机推荐

  1. 数据结构看书笔记(二)--算法(Algorithm)简介

    算法:是解决问题求解步骤的描述,在计算机中表现为指令的有限序列,并且每条指令表示一个或多个操作. 算法的特性:算法具有五个特性:输入.输出.有穷性.确定性.可行性 输入输出:算法具有零个或多个输入:至 ...

  2. WORD学习之插入分页符

    我们在用Word编辑文档时,不可避免的需要对文档进行页面布局,才能使得文档看起来更加美观.有时候会使用到分页符,下面就简单介绍一下分页符 分页符 主要作用: 1.若要把两段分开在两页显示时,把光标定位 ...

  3. 比较器:Compare接口与Comparator接口区别与理解

    一.实现Compare接口与Comparator接口的类,都是为了对象实例数组排序的方便,因为可以直接调用 java.util.Arrays.sort(对象数组名称),可以自定义排序规则. 不同之处: ...

  4. 存储过程中的output跟return区别及实例说明

    存储过程return,表示该存储过程执行到当当前return位置,不再向下执行: 存储过程写法:set ANSI_NULLS ON set QUOTED_IDENTIFIER ON GO ALTER ...

  5. AppSettings从数据库读取

    /// <summary> /// 提供对配置信息的访问 /// </summary> public static class AppSettings { /// <su ...

  6. 未能加载文件或程序集“projectname, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”或它的某一个依赖项。系统找不到指定的文件。

  7. table 边框显示

    .td{border:solid #add9c0; border-width:0px 1px 1px 0px;}.table{border:solid #add9c0; border-width:1p ...

  8. java String 详解

    1.java语言的字符串序列是通过字符串类实现的.java提供了3个字符串类:String类.StringBuilder类和StringBuffer类.String类是不变字符串,StringBuff ...

  9. MVC 中301永久重定向

    public class PermanentRedirectResult : ViewResult { public string Url { get; set; } public Permanent ...

  10. windowsAPI遍历文件夹(速度高于递归)

    #region API 遍历文件夹及其子文件夹和子文件 #region 声明WIN32API函数以及结构 ************************************** [DllImpo ...