先来说说自定义指令

ng通过内置指令的方式实现了对html的增强,同时也赋予了我们自定义指令的功能,让我们根据实际需求进行指令的定制。自定义指令涉及到模板(template)、作用域(scope)、数据绑定和Dom操作等内容,我也是正在学习中,写的比较肤浅。

如何自定义指令

从简单的写起,我们先来认识几个常用的配置吧,深入点的我也不会哈。。。

   App.directive("directiveName",function(){
return {
//指令可用在何处,一般有E(Elemenmt)、A(Attribute)、C(Class),还有一个注释,这个就算了吧。默认为A,也就是作为属性用。
restrict: 'E',
//是否替换指令元素。一般这样调用元素<dialog></dialog>,当为true的时候,页面就不会出现dialog元素。
replace: true,
//指令模板,一般模板复杂的话,可以单独放置一个文件,然后使用templateUrl进行引用。
template: '<div>。。。</div>',
//这儿是作用域,如果没有这个配置,则公用所在controller的域;如果有scope,则起到了隔离作用,这儿的key是template的表达式,value指的是元素的属性key。
scope: {
key:value
}
};
});

只要这么个定义了,页面上就可以高兴的使用啦。下面来看几个例子吧,实践出真知啊。

例子1.简单的对话框,这个例子有点挫

<!DOCTYPE html>

<html ng-app="App">
<head>
<link rel="stylesheet" type="text/css" href="http://sandbox.runjs.cn/uploads/rs/394/xjz9g1bv/bootstrap.css">
<script type="text/javascript" src="http://sandbox.runjs.cn/uploads/rs/394/xjz9g1bv/angular.js"></script>
<script type="text/javascript"> var App = angular.module("App", []); App.controller("ctrl", function ($scope) { }); App.directive("dialog",function(){
return {
//限制,也就是这个指令可用在何处,一般有E(Elemenmt)、A(Attribute)、C(Class),还有一个注释,这个就算了吧。默认为A,也就是作为属性用。
restrict: 'E',
//是否替换指令元素。一般这样调用元素<dialog></dialog>,当为true的时候,页面就不会出现dialog元素。
replace: true,
//指令模板,一般模板复杂的话,可以单独放置一个文件,然后使用templateUrl进行引用。
template: '<div><div>{{dialogTitle}}</div><div>{{dialogContent}}</div></div>',
//这儿是作用域,如果没有这个配置,则公用所在controller的域;如果有scope,则起到了隔离作用,这儿的key是template的表达式,value指的是元素的属性key。
scope: {
//dialogTitle指的是template的{{dialogTitle}}
//title是<dialog title='标题'></dialog>中的title;如果元素是这样的:<dialog dialogTitle='标题'></dialog>,则下面的配置可以简写成:dialogTitle:'@'
dialogTitle: '@title',
dialogContent:'@content'
}
};
}); </script>
</head>
<body style='padding-top:10px;'>
<div class='container' ng-controller="ctrl">
<dialog title='我是标题' content='这个例子有点挫。。。'></dialog>
</div>
</body>
</html>

查看效果:

点击这里查看效果。

这个例子真的好搓啊,内容还有点,样式真是挫,人真的是很感性的啊。估计这个例子大家看不下去了,我来找找Bootstrap的dialog样式吧,看看下个例子吧。

例子2.有模有样的对话框

在这个例子里,我们要使用Bootstrap的模态框相关的样式。额,我们先来看下Bootstrap对模态框的定义:

<div class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
<h4 class="modal-title">Modal title</h4>
</div>
<div class="modal-body">
<p>One fine body&hellip;</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->

纳尼,这么复杂,这要让我写到template模板里,估计是要写疯了。上面介绍了,template还有个兄弟,叫templateUrl,这个似乎是个地址,可以将复杂的模板内容写到其中,然后链接一下,人生似乎好起来了。

这儿再来点变动,就是:dialog的内容,我想写成这样<dialog>我是内容啊,内容。。。</dialog>,这儿就会用到transclude属性了,当transclude为true的时候,template中应至少有一个根元素,而且内部要有元素有ng-transclude属性,这样才能起来替换作用。

好吧,扯了这么多,估计也没看到,还是来看看代码吧。

<!DOCTYPE html>

<html ng-app="App">
<head>
<link rel="stylesheet" type="text/css" href="http://sandbox.runjs.cn/uploads/rs/394/xjz9g1bv/bootstrap.css">
<script type="text/javascript" src="http://sandbox.runjs.cn/uploads/rs/394/xjz9g1bv/angular.js"></script>
<script type="text/ng-template" id="dialogDirective">
<div class="modal show">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title">{{title}}</h4>
</div>
<div class="modal-body" ng-transclude> </div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" >关闭</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
</script>
<script type="text/javascript"> var App = angular.module("App", []); App.controller("ctrl", function ($scope) { }); App.directive("dialog",function(){
return {
restrict: 'E',
replace: true,
templateUrl: 'dialogDirective',
//transclude是必须的。
transclude:true,
scope: {
title: '@',
}
}; }); </script>
</head>
<body style='padding-top:10px;'>
<div class='container' ng-controller="ctrl">
<dialog title='我是标题'>
我是内容啊,我是内容,请回答。。。
</dialog>
</div>
</body>
</html>

效果:

点击这里查看效果。

额,效果比上一个似乎好多了呀,但是“关闭”按钮为什么不能用呢???

本来这篇想这么结束的,既然你问到了,那就只好再来一段了,且看下个例子

例子3.带关闭功能的对话框呀

我们上面的例子里对话框倒是美美的,但是关闭呢,为啥关闭不了,这儿又要用到一个新的属性了,link。例子里会充分体现link的用处:

<!DOCTYPE html>

<html ng-app="App">
<head>
<link rel="stylesheet" type="text/css" href="http://sandbox.runjs.cn/uploads/rs/394/xjz9g1bv/bootstrap.css">
<script type="text/javascript" src="http://sandbox.runjs.cn/uploads/rs/394/xjz9g1bv/angular.js"></script>
<script type="text/ng-template" id="dialogDirective">
<div class='modal show'>
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" ng-click="close();"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">{{title}}</h4>
</div>
<div class="modal-body" ng-transclude> </div>
<div class="modal-footer">
<!--注意:此处调用的是link方法中scope定义的close方法-->
<button type="button" class="btn btn-primary" ng-click="close();" >关闭</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
</script>
<script type="text/javascript"> var App = angular.module("App", []); App.controller("ctrl", function ($scope) { }); App.directive("dialog",function(){
return {
restrict: 'E',
replace: true,
templateUrl: 'dialogDirective',
//transclude是必须的。
transclude:true,
//指令中定义了scope,起到了隔离作用,和ctrl中的scope已经不相关了。
scope: {
title: '@',
},
link:function(scope,elements,attributes){
//注意:因为指令里定义了scope,所以link函数中的scope只包含指令中title,对于dom的行为,应在link函数中定义。
scope.close=function(){
elements.removeClass('show');
}
}
}; }); </script>
</head>
<body style='padding-top:10px;'>
<div class='container' ng-controller="ctrl">
<dialog title='我是标题'>
我是内容啊,我是内容,请回答。。。
</dialog>
</div>
</body>
</html>

效果:

点击这里查看效果。

小结

本篇只是大致了解了自定义指令,下一步,我们还要深入了解自定义指令的相关应用。

AngularJS学习笔记(五)自定义指令(1)的更多相关文章

  1. AngularJs学习笔记3——自定义指令

    指令 概述: 前面也说过一些常用指令,用于快速入门.现在详细总结一下:指令用于实现各种页面的操作,是对于底层DOM操作的封装,扩展了HTML的行为,实现页面交互以及数据绑定. 指令是一种执行的信号,一 ...

  2. AngularJS学习笔记(四) 自定义指令

    指令(directive)是啥?简单来说就是实现一定功能的XXX...之前一直用的ng-model,ng-click等等都是指令.当我有一个ng没提供的需求的时候,就可以自定义个指令.指令的好处显而易 ...

  3. AngularJs学习笔记5——自定义服务

    前面整理了AngularJs双向数据绑定和自定义指令的相关内容,从手册上看也知道,ng部分还包括过滤器和函数,以及服务等. 过滤器:filter,就是对数据进行格式化,注意管道格式,例如: {{表达式 ...

  4. 【AngularJS学习笔记】01 指令、服务和过滤器

    AngularJS 指令是扩展的 HTML 属性,带有前缀 ng-. 比如: ng-app 指令初始化一个 AngularJS 应用程序.注意ng-app一般为空,如果值不为空,就得加这样一句代码va ...

  5. AngularJs学习笔记--Forms

    原版地址:http://code.angularjs.org/1.0.2/docs/guide/forms 控件(input.select.textarea)是用户输入数据的一种方式.Form(表单) ...

  6. AngularJs学习笔记--concepts(概念)

    原版地址:http://code.angularjs.org/1.0.2/docs/guide/concepts 继续.. 一.总括 本文主要是angular组件(components)的概览,并说明 ...

  7. angular学习笔记(三十)-指令(6)-transclude()方法(又称linker()方法)-模拟ng-repeat指令

    在angular学习笔记(三十)-指令(4)-transclude文章的末尾提到了,如果在指令中需要反复使用被嵌套的那一坨,需要使用transclude()方法. 在angular学习笔记(三十)-指 ...

  8. AngularJs学习笔记--IE Compatibility 兼容老版本IE

    原版地址:http://docs.angularjs.org/guide/ie Internet Explorer Compatibility 一.总括 这文章描述Internet Explorer( ...

  9. AngularJs学习笔记--Understanding the Controller Component

    原版地址:http://docs.angularjs.org/guide/dev_guide.mvc.understanding_model 在angular中,controller是一个javasc ...

  10. [转载]SharePoint 2013搜索学习笔记之自定义结果源

    搜索中心新建好之后在搜索结果页上会默认有所有内容,人员,对话,视频这四个结果分类,每个分类会返回指定范围的搜索结果,这里我再添加了部门日志结果分类,搜索这个分类只会返回部门日志内容类型的搜索结果,要实 ...

随机推荐

  1. 根据cxgrid的filterControl建立强大灵活的过滤器

  2. 【推荐】介绍两款Windows资源管理器,Q-Dir 与 FreeCommander XE(比TotalCommander更易用的免费资源管理器)

    你是否也像我一样,随着硬盘.文件数量的增加,而感到对于文件的管理越来越乏力. 于是我试用了传说中的各种软件,包括各种Explorer外壳,或者第三方资源管理器. 最后我确定下来经常使用,并推荐给您的是 ...

  3. 绿色版Mysql自动建立my.ini和命令行启动并动态指定datadir路径

    1.先去下载绿色版的Mysql(https://cdn.mysql.com//archives/mysql-5.7/mysql-5.7.20-winx64.zip) 2.解压缩到任意目录(如D:\My ...

  4. SoftWareHelper

    SoftWareHelper 环境 Visual Studio 2017,.Net Framework 4.0 SDK GitHub源码:https://github.com/yanjinhuagoo ...

  5. WPF 分享一种背景动画效果

    今天看微软的一个Samples,发现一个蛮好玩的背景样式,如下图所示: 风格比较卡哇伊. <Window x:Class="WPFSamplesTest.MainWindow" ...

  6. 在.net中修改Webbrowser控件的IE版本

    根据32位.64位系统来分别修改对应的注册表路径的键值对,不需要重启程序. /// <summary> /// 修改Webbrowser控件模拟的IE版本 /// </summary ...

  7. JavaScript 知识

    1. js中this表示当前标签,获取当前标签内的属性,示例如下: var user_id = $(this).attr("data-user-id"); 2.   * js中va ...

  8. BZOJ 5194--[Usaco2018 Feb]Snow Boots(STL)

    5194: [Usaco2018 Feb]Snow Boots Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 81  Solved: 61[Submi ...

  9. 02_python_while循环/格式化输出/逻辑运算

    一. while循环 1.基本形式 while 条件: 循环体 # 判断条件是否为真,如果真,执行代码块.然后再次判断条件是否为真.如果真继续执行代码块...直到条件变成了假.循环退出 ps:死循环 ...

  10. Python 绝技 —— TCP服务器与客户端

    i春秋作家:wasrehpic 0×00 前言 「网络」一直以来都是黑客最热衷的竞技场.数据在网络中肆意传播:主机扫描.代码注入.网络嗅探.数据篡改重放.拒绝服务攻击……黑客的功底越深厚,能做的就越多 ...