angularjs 指令详解 - template, restrict, replace
通过指令机制,angularjs 提供了一个强大的扩展系统,我们可以通过自定义指令来扩展自己的指令系统。
怎样定义自己的指令呢?
我们通过 Bootstrap UI来学习吧。这个项目使用 angularjs 将 Bootstrap 3 进行了封装,是我们学习 angularjs 很好的样例。
从 Alert 开始
首先,我们从比较简单的 alert 指令来开始学习。
在 Bootstrap 中,警告框使用类 alert 来表示, 通过 alert-success, alert-info, alert-warning, alert-danger 来表示不同类型的警告。
<div class="alert alert-success">...</div>
<div class="alert alert-info">...</div>
<div class="alert alert-warning">...</div>
<div class="alert alert-danger">...</div>
对于可关闭的警告框来说,还可以用一个可选的.alert-dismissable和关闭按钮,但是,这是通过脚本来实现的,在 bootstrap ui 这里没有使用。
<div class="alert alert-warning alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<strong>Warning!</strong> Best check yo self, you're not looking too good.
</div>
bootstrap UI 中的用法
在 Bootstrap UI 的示例中,我们可以直接通过自定义的 alert 来定义一个提示。其中的 type 用来定义提示的类型,提示的信息内容通过绑定来处理。
<div ng-controller="AlertDemoCtrl">
<alert ng-repeat="alert in alerts" type="alert.type" close="closeAlert($index)">{{alert.msg}}</alert>
<button class='btn btn-default' ng-click="addAlert()">Add Alert</button>
</div>
对应的脚本
function AlertDemoCtrl($scope) {
$scope.alerts = [
{ type: 'danger', msg: 'Oh snap! Change a few things up and try submitting again.' },
{ type: 'success', msg: 'Well done! You successfully read this important alert message.' }
];
$scope.addAlert = function() {
$scope.alerts.push({msg: "Another alert!"});
};
$scope.closeAlert = function(index) {
$scope.alerts.splice(index, 1);
};
}
bootstrap UI 中的定义
下面就是指令的定义了。
angular.module('ui.bootstrap.alert', [])
.controller('AlertController', ['$scope', '$attrs', function ($scope, $attrs) {
$scope.closeable = 'close' in $attrs;
}])
.directive('alert', function () {
return {
restrict:'EA',
controller:'AlertController',
templateUrl:'template/alert/alert.html',
transclude:true,
replace:true,
scope: {
type: '@',
close: '&'
}
};
});
最终生成的标记为:
<div ng-controller="AlertDemoCtrl" class="ng-scope">
<!-- ngRepeat: alert in alerts -->
<div class="alert ng-isolate-scope alert-danger" ng-class=""alert-" + (type || "warning")" ng-repeat="alert in alerts" type="alert.type" close="closeAlert($index)">
<button ng-show="closeable" type="button" class="close" ng-click="close()">×</button>
<div ng-transclude=""><span class="ng-scope ng-binding">Oh snap! Change a few things up and try submitting again.</span></div>
</div> <!-- end ngRepeat: alert in alerts -->
<div class="alert ng-isolate-scope alert-success" ng-class=""alert-" + (type || "warning")" ng-repeat="alert in alerts" type="alert.type" close="closeAlert($index)">
<button ng-show="closeable" type="button" class="close" ng-click="close()">×</button>
<div ng-transclude=""><span class="ng-scope ng-binding">Well done! You successfully read this important alert message.</span></div>
</div><!-- end ngRepeat: alert in alerts -->
<button class="btn btn-default" ng-click="addAlert()">Add Alert</button>
</div>
看不懂!没有关系,我们慢慢来。
从模版开始
比如说,在下面的代码中,你希望通过 my-customer 将一个内容为空白的 div 变成内容为绑定名字和地址的文本内容
<div ng-controller="Ctrl">
<div my-customer></div>
</div>
比如生成下面的结果:
<div ng-controller="Ctrl">
<div>Name: {{customer.name}} Address: {{customer.address}}</div>
</div>
内嵌模版
对于以上的要求,我们可以定义一个名为 my-customer 的指令。然后,说明这个指令将会生成元素中间的内容,我们称之为模版的内容。
angular.module('docsSimpleDirective', [])
.controller('Ctrl', function($scope) {
$scope.customer = {
name: 'Naomi',
address: '1600 Amphitheatre'
};
})
.directive('myCustomer', function() {
return {
template: 'Name: {{customer.name}} Address: {{customer.address}}'
};
});
directive 表示我们要定义一个自定义的指令,指令的名称为 myCustomer,后面的函数用来定义指令的特征
template 表示我们的指令使用的模版,你会看到,它的值就是我们准备生成的内容。
不过,你会说,且慢,我希望使用 myCustomer 元素,而不是 Attribute ,那又该怎么办呢?
默认情况下,指令只能作为元素的 attribute 使用,如果希望用在其它的地方,那么你需要学习第二个属性 restrict
指令的使用范围
restrict 的取值可以有三种:
- A 用于元素的 Attribute,这是默认值
- E 用于元素的名称
- C 用于 CSS 中的 class
比如说,我们这样定义指令。
var app = angular.module("app", [])
.directive("hello", function () {
var option = {
restrict: "AEC",
template: "Hello, Directive",
};
return option;
})
由于我们指定了可以用于三种情况下,那么,就可以如下三种形式来使用这个指令
<!-- 元素 -->
<div>
<hello></hello>
</div> <!-- 属性-->
<div>
<div hello></div>
</div> <!-- class -->
<div>
<div class="hello"></div>
</div>
输出的结果分别如下:
<!-- 元素 -->
<div>
<hello>Hello, Directive</hello>
</div>
<!-- 属性-->
<div>
<div hello="">Hello, Directive</div>
</div>
<!-- class -->
<div>
<div class="hello">Hello, Directive</div>
</div>
替换元素
下一个你需要知道的参数为 replace,顾名思义,这是替换的意思,默认为 false,就是将模版的内容追加到元素中,如果设置为 true,那么模版的内容将会替换元素的内容。
原来的输出将会成为下面的样子,实际上,你在页面中将会看不到输出的内容,hello 元素浏览器是不认识的,而其它两种方式,我们又没有生成任何的元素。
<!-- 元素 -->
<div>
<hello></hello>
</div>
<!-- 属性-->
<div>
<div hello=""></div>
</div>
<!-- class -->
<div>
<div class="hello"></div>
</div>
如果我们将模版更新为一个元素,如下所示。
var app = angular.module("app", [])
.directive("hello", function () {
var option = {
restrict: "AECM",
template: "<h3>Hello, Directive</h3>",
replace: true
};
return option;
})
这次,你将会看到在 replace 为 true 情况下的元素了。
<!-- 元素 -->
<div>
<h3>Hello, Directive</h3>
</div>
<!-- 属性-->
<div>
<h3 hello="">Hello, Directive</h3>
</div>
<!-- class -->
<div>
<h3 class="hello">Hello, Directive</h3>
</div>
transclusion 嵌入
如果我们在上面的元素中这么写会怎样呢?
<!-- 元素 -->
<div>
<hello>12345678</hello>
</div>
你会发现 12345678 消失了,这个元素完全被模版替换了。如果我们需要保留这些内容怎么处理呢?
transclusion 指的是定义模版的元素如何处理问题,比如,在使用指令的时候,指令中包含了内容,那么这些内容我们是否直接被替换为模版,还是将这些内容嵌入到模版中。
在使用它的时候,需要在两个地方说明,一是在指令中说明需要嵌入,二是在模版中说明嵌入到哪里。
var app = angular.module("app", [])
.directive("hello", function () {
var option = {
restrict: "AECM",
template: "<h3>Hello, Directive, <span ng-transclude></span></h3>",
replace: true,
transclude: true
};
return option;
})
然后,在模版中说明嵌入的位置。
template: "<h3>Hello, Directive, <span ng-transclude></span></h3>",
页面中的使用。
<hello>12345678</hello>
最后,生成的结果如下。
<h3>Hello, Directive, <span ng-transclude=""><span class="ng-scope">12345678</span></span></h3>
看看 alert 使用的模版
模版的实际定义如下。
<div class="alert" ng-class="{'alert-{{type || 'warning'}}': true, 'alert-dismissable': closeable}" role="alert">
<button ng-show="closeable" type="button" class="close" ng-click="close()">
<span aria-hidden="true">×</span>
<span class="sr-only">Close</span>
</button>
<div ng-transclude></div>
</div>
在生成之后,alert 被替换为了 div 元素,至于关闭按钮是否显示出来,取决于 closeable 属性的值。
replace 定义为真,表示使用模版的定义替换原来的元素,而 transclude 用来把内容填充到目标位置。在内嵌的 div 元素中使用了 ng-transclude 指示了填充的目标位置。
angularjs 指令详解 - template, restrict, replace的更多相关文章
- AngularJS指令详解
一.什么是指令? 在<AngularJs权威教程>中,指令可以简单理解成特定的DOM元素上运行的函数:我认为还可以理解成将将自定义的HTML标签解析成原始的标签,然后为其加入一些扩展的功能 ...
- angularjs 指令详解
一.指令定义 对于指令,可以把它简单的理解成在特定DOM元素上运行的函数,指令可以扩展这个元素的功能. 首先来看个完整的参数示例再来详细的介绍各个参数的作用及用法: <div my-direct ...
- 迈向angularjs2系列(2):angular2指令详解
一:angular2 helloworld! 为了简单快速的运行一个ng2的app,那么通过script引入预先编译好的angular2版本和页面的基本框架. index.html: <!DOC ...
- [转]JVM指令详解(上)
作者:禅楼望月(http://www.cnblogs.com/yaoyinglong) 本文主要记录一些JVM指令,便于记忆与查阅. 一.未归类系列A 此系列暂未归类. 指令码 助记符 ...
- C#中的预处理器指令详解
这篇文章主要介绍了C#中的预处理器指令详解,本文讲解了#define 和 #undef.#if.#elif.#else和#endif.#warning和#error.#region和#endregio ...
- rsync指令详解
rsync指令详解(更详细的看官方文档http://rsync.samba.org/ftp/rsync/rsync.html) [root@Centos epel]# rsync --help rsy ...
- #pragma 预处理指令详解
源地址:http://blog.csdn.net/jx_kingwei/article/details/367312 #pragma 预处理指令详解 在所有的预处理指令中, ...
- LDM与STM指令详解
title: LDM与STM指令详解 date: 2019/2/26 17:58:00 toc: true --- LDM与STM指令详解 指令形式如下,这里的存储方向是针对寄存器的 Load Mul ...
- C#中的预处理指令详解
这篇文章主要介绍了C#中的预处理指令详解,本文讲解了#define 和 #undef.#if.#elif.#else和#endif.#warning和#error.#region和#endregion ...
随机推荐
- shell循环语句
所有的笔记只记录一些例子,根据例子解释一些出现的语法,不介绍具体的语法 2015-07-01 21:58:33 星期三 for循环 用例一用for循环在家目录下创建aaa1-aaa10,然后在aaa1 ...
- 如何在vim里删除空行?
删除空行,进入底行模式 :g/^$/d ^代表首列 $代表尾列 d代表删除 g代表全局替换
- IIS上虚拟站点的web.config与主站点的web.config冲突解决方法 分类: ASP.NET 2015-06-15 14:07 60人阅读 评论(0) 收藏
IIS上在主站点下搭建虚拟目录后,子站点中的<system.web>节点与主站点的<system.web>冲突解决方法: 在主站点的<system.web>上一级添 ...
- Swift学习之熟悉控件
最近是比较清闲一些的,对于一个开发者来说,这也是一个很好的充电机会.以前做项目都是使用Objective-C去开发,但我们都知道,Swift语言从2014年的出现到现在,一步一步变的完善,渐渐变的受欢 ...
- nginx:文件下载指定保存文件名的配置
一般在我们上传完资源文件之后,为了避免文件名冲突,会将文件名改成毫无意义的一段字符.这个字符,可能是MD5产生的,或者是其他方式产生的字符串.这时候,下载的时候,默认保存的文件名会是这段毫无意义的文件 ...
- 使用nose 进行Python项目的自动化测试
一.为什么使用nose? 编写测试更容易.nose可以自动识别继承于unittest.TestCase的测试单元,并执行测试,而且,nose也可以测试非继承于unittest.TestCase的测试单 ...
- SSH私钥取消密码(passphrase )
1. 使用openssl命令去掉私钥的密码 openssl rsa -in ~/.ssh/id_rsa -out ~/.ssh/id_rsa_new 2. 备份旧私钥 mv ~/.ssh/id_rsa ...
- mssql查询列名中包含特定字段的列
CREATE TABLE itemdata_LANG ( itemno ) NOT NULL, itemname ), -- 产品名称 othername ), indications ), -- 适 ...
- IE6中内容高度比高级浏览器高的解决办法
1.div高度小于12px时,加over-flow:hidden; 2.多用padding,少用margin: 3.img vertical-align:top;
- 解决IE6/IE7/IE8不支持before,after问题
对从事web开发的朋友来讲,低版本的IE永远是一个痛点,不支持最新技术(如css3,html5). 在现在web开发中使用图标字体已经很广泛,如Font Awesome,Bootstrap等,字体图片 ...