参考:

https://docs.angularjs.org/api/ng/service/$compile

http://www.zouyesheng.com/angular.html

Directive (指令),是Angular最为强大和复杂的部分。directive可以扩展丰富html的行为。

举个例子,如果我们想让html某元素在屏幕上居中显示,我们无需知道屏幕窗口实际的宽度,只需加上align="center"属性就能达到目的。

这是html提供给我们好的接口行为。但是如果想要元素在屏幕的1/3位置显示,这就有些困难了。因为html并没有提供给我们这种语法。

我们可以通过directive定义一些新的行为,然后让Angular提供的HTML compiler(编译器)去解析并编译行为。更进一步,当我们开发应用系统,我们甚至可以为该应用创建特定的directive。

即Domain Specific Language 领域特定语言。

使用指令可以增强复用性。节省很多代码。

定义指令可以使用下面的写法模板

var myModule = angular.module(...);
myModule.directive('directiveName', function factory(injectables) {
var directiveDefinitionObject = {
priority: 0,
template: '<div></div>',
templateUrl: 'directive.html',
replace: false,
transclude: false,
restrict: 'A',
scope: false,
compile: function compile(tElement, tAttrs, transclude) {
return {
pre: function preLink(scope, iElement, iAttrs, controller) { ... },
post: function postLink(scope, iElement, iAttrs, controller) { ... }
}
},
link: function postLink(scope, iElement, iAttrs) { ... }
};
return directiveDefinitionObject;
});

由上可知,定义指令,需要返回一个对象。对象中包含很多属性如restrict,replace等。下面根据例子介绍每个属性的用法。

例1

See the Pen 1. basic by finley (@mafeifan) on CodePen.

如下有两个指令,分别是元素类型和属性类型。

<my-directive><a href="http://google.com">Click me to go to Google</a></my-directive>
<p my-directive=""><a href="http://google.com">Click me to go to Google</a></p>

生成的html都是超链接。

参数restrict:个人理解指令的使用方式。可选值 EACM。分别代表 element,attribute,class和comment。

  • E 元素方式 <my-directive></my-directive>
  • A 属性方式 <div my-directive="exp"> </div>
  • C 类方式 <div class="my-directive: exp;"></div>
  • M 注释方式 <!-- directive: my-directive exp -->

参数template:要替换的内容。

参数templateUrl:从指定的url读模版内容,如果内容很长或者需要复用就用这个参数吧。比如我们可以写成

templateUrl : "../myTemplate.html"
// 或者动态获取
templateUrl: function(element, attrs) {
return attrs.templateUrl || '../../uib/template/alert/alert.html';
},

参数replace:是否使用模板内容替换掉整个节点, true 替换整个节点, false 替换节点内容。如例1,若replace为true。则生成的html结构如下:

<a href="http://google.com">Click me to go to Google</a>
<a href="http://google.com" my-directive="">Click me to go to Google</a>

参数link:

例2 link方法

See the Pen Directive/2 link by finley (@mafeifan) on CodePen.

参数scope:绑定策略

参数compile和 link。分别代表编译和链接。

例3 绑定

如下TestCtrl里div元素有4个属性。a,abc,xx,c

  <body ng-app="myApp">
<div ng-controller="TestCtrl">
<div a abc="here" xx="{{ a }}" c="ccc"></div>
</div>
</body>

JS

angular.module('myApp',[])
.controller('TestCtrl', function($scope){
$scope.a = '123';
$scope.here = 'here ~~';
}) .directive('a', function(){
var func = function(element, attrs, link){
return function(scope){
/** 输出结果
a: "here"
b: "123"
c: "ccc"
d: "ccc"
e: "here ~~
*/
console.log(scope);
};
};
return {
restrict: 'A',
compile: func,

// a 找到属性名为abc,其值为here
     // b 找到属性名为xx,其值为{{a}} 接着找$scope.a 存在,其值为 123
     // c @attrName 没有写attrName, 默认取自己的值,等价于@c ,找到属性c,其值为ccc
     // d 如上
     // e '=abc' 把属性abc的值当作scope的属性名。 这里存在属性abc,其值为here。存在$scope.here。最终值为'here ~~'
     // 若改为abc={{ here }} 效果跟 b: '@xx'一样

    scope: {a: '@abc', b: '@xx', c: '@', d: '@c', e: '=abc'}
};
});

例4 transclude

See the Pen NG Directive学习4 transclude by finley (@mafeifan) on CodePen.

AngularJS中Directive指令系列 - 基本用法的更多相关文章

  1. AngularJS中Directive指令系列 - scope属性的使用

    文章是转的,我做下补充.原文地址:https://segmentfault.com/a/1190000002773689 每当一个指令被创建的时候,都会有这样一个选择,是继承自己的父作用域(一般是外部 ...

  2. AngularJS中Directive指令系列

    近段时间在研究Angular中的directive用法,打算写个系列.以官方文档为主.并参考诸多教程.加上自己的思考. 基本概念及用法 scope属性的使用.  &, <, =, @ 符 ...

  3. AngularJS中Directive指令系列 - bindToController属性的使用

    默认false.这个属性用来绑定scope的属性直接赋给controller.可以为true或者和scope相同格式的对象. 此外使用此属性,要设置controller的别名,通常通过"co ...

  4. angularjs中directive指令与component组件有什么区别?

     壹 ❀ 引 我在前面花了两篇博客分别系统化介绍了angularjs中的directive指令与component组件,当然directive也能实现组件这点毋庸置疑.在了解完两者后,即便我们知道co ...

  5. angularjs学习之六(angularjs中directive指令的一般编程事件绑定 模板使用等)

    angular js 中模板的使用.事件绑定以及指令与指令之间的交互 相应教学视频地址(需FQ):v=aG8VD0KvUw4">angularjs教学视频 <!doctype h ...

  6. AngularJS中的指令全面解析(转载)

    说到AngularJS,我们首先想到的大概也就是双向数据绑定和指令系统了,这两者也是AngularJS中最为吸引人的地方.双向数据绑定呢,感觉没什么好说的,那么今天我们就来简单的讨论下AngularJ ...

  7. angularJS中directive父子组件的数据交互

    angularJS中directive父子组件的数据交互 1. 使用共享 scope 的时候,可以直接从父 scope 中共享属性.使用隔离 scope 的时候,无法从父 scope 中共享属性.在 ...

  8. angularJS中directive与controller之间的通信

    当我们在angularJS中自定义了directive之后需要和controller进行通讯的时候,是怎么样进行通讯呢? 这里介绍3种angular自定义directive与controller通信的 ...

  9. AngularJS中的指令

    欢迎大家讨论与指导 : )  前言 当AngularJS中的内置指令不能满足我们的需求,或者当我们需要创建一个能够用于多个AngularJS程序的自包含的功能单元时,我们应该创建自定义指令来满足需求. ...

随机推荐

  1. Python 模块化 from .. import 语句资源搜索顺序 (三)

    接着上一篇文章最后的import子句资源搜索顺序,我们来写几个例子了解下. 例一. #test1.py x = 123 #test.py import test1 print(dir()) print ...

  2. PDF压缩,在线压缩免费

    https://smallpdf.com/ 一个很牛逼的网站 https://zh.wikihow.com/ https://zh.wikihow.com/%E9%A6%96%E9%A1%B5

  3. Java SPI(Service Provider Interface)

    SPI 全称为 (Service Provider Interface) ,是JDK内置的一种服务提供发现机制. 目前有不少框架用它来做服务的扩展发现, 简单来说,它就是一种动态替换发现的机制, 举个 ...

  4. Http请求发送json数据用实体类接收

    以上是请求URL以及json数据 接收层

  5. 分布式消息通信ActiveMQ

    消息中间件 消息中间件是指利用高效可靠的消息传递机制进行平台无关的数据交流,并且基于数据通信来进行分布式系统的集成.通过提供消息传递和消息排队模型,可以在分布式架构下扩展进程之间的通信. 消息中间件能 ...

  6. Web框架——XWAF的代码结构和运行机制(4)

    XWAF是一套基于Servlet和java反射技术的Web应用程序框架,它利用Servlet运行机制在服务器上加载和运行,接管客户端请求,依靠ServletRequest对象获取用户请求信息,使用Se ...

  7. 分布式一致性协议-2PC与3PC(二)

    一.分布式一致性 一个事务需要跨多个分布式节点,又要保持事务的ACID特性,需要引入协调者来统一调度所有分布式节点的执行逻辑,被调度的节点称为参与者. 协调者负责调用参与者,并决定最终是否提交事务.基 ...

  8. iOS 使约束带动画效果(Animate NSLayoutconstraints)

    http://stackoverflow.com/questions/12926566/are-nslayoutconstraints-animatable http://stackoverflow. ...

  9. 关于 web.config impersonate 帐号模拟

    1.模拟 IIS 验证的帐户或用户 若要在收到 ASP.NET 应用程序中每个页的每个请求时模拟 Microsoft Internet 信息服务 (IIS) 身份验证用户,必须在此应用程序的 Web. ...

  10. GoogleTest初探(0)

    单元测试是一种保证代码质量的手段.程序员可以通过写单元测试来保证自己写的代码的功能正确. 本人所在公司使用GoogleTest测试框架来进行单元测试.虽然现在在公司的工程代码中写单元测试已经驾轻就熟, ...