AngularJS中Directive指令系列 - 基本用法
参考:
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指令系列 - 基本用法的更多相关文章
- AngularJS中Directive指令系列 - scope属性的使用
文章是转的,我做下补充.原文地址:https://segmentfault.com/a/1190000002773689 每当一个指令被创建的时候,都会有这样一个选择,是继承自己的父作用域(一般是外部 ...
- AngularJS中Directive指令系列
近段时间在研究Angular中的directive用法,打算写个系列.以官方文档为主.并参考诸多教程.加上自己的思考. 基本概念及用法 scope属性的使用. &, <, =, @ 符 ...
- AngularJS中Directive指令系列 - bindToController属性的使用
默认false.这个属性用来绑定scope的属性直接赋给controller.可以为true或者和scope相同格式的对象. 此外使用此属性,要设置controller的别名,通常通过"co ...
- angularjs中directive指令与component组件有什么区别?
壹 ❀ 引 我在前面花了两篇博客分别系统化介绍了angularjs中的directive指令与component组件,当然directive也能实现组件这点毋庸置疑.在了解完两者后,即便我们知道co ...
- angularjs学习之六(angularjs中directive指令的一般编程事件绑定 模板使用等)
angular js 中模板的使用.事件绑定以及指令与指令之间的交互 相应教学视频地址(需FQ):v=aG8VD0KvUw4">angularjs教学视频 <!doctype h ...
- AngularJS中的指令全面解析(转载)
说到AngularJS,我们首先想到的大概也就是双向数据绑定和指令系统了,这两者也是AngularJS中最为吸引人的地方.双向数据绑定呢,感觉没什么好说的,那么今天我们就来简单的讨论下AngularJ ...
- angularJS中directive父子组件的数据交互
angularJS中directive父子组件的数据交互 1. 使用共享 scope 的时候,可以直接从父 scope 中共享属性.使用隔离 scope 的时候,无法从父 scope 中共享属性.在 ...
- angularJS中directive与controller之间的通信
当我们在angularJS中自定义了directive之后需要和controller进行通讯的时候,是怎么样进行通讯呢? 这里介绍3种angular自定义directive与controller通信的 ...
- AngularJS中的指令
欢迎大家讨论与指导 : ) 前言 当AngularJS中的内置指令不能满足我们的需求,或者当我们需要创建一个能够用于多个AngularJS程序的自包含的功能单元时,我们应该创建自定义指令来满足需求. ...
随机推荐
- [SHOI2010]最小生成树
题目 首先让其余所有边都减\(1\)和让自己加\(1\)没什么区别 考虑\(kruskal\)的过程 首先边权大于这条边的是不用考虑的 考虑把那些边权比这条边小的调节到比这条边大,这样就相当于在生成树 ...
- jQuery中异步请求
1.load方法 使用load()方法通过Ajax请求加载服务器中的数据,并把返回的数据放置到指定的元素中,它的调用格式为: $(selector).load(URL,data,callback); ...
- 【JavaScript】插件参数的写法
就是实现复制的一个过程 (function() { var Explode = function(container, params) { 'use strict'; var n = this; if ...
- nRF5 SDK for Mesh(八) Exploring Mesh APIs using light switch example,使用 灯开关 案例探索BLE mesh 的APIS
Exploring Mesh APIs using light switch example The light switch example is meant to showcase the API ...
- c#将List转换成DataTable(采用Emit)
前段时间通过网上查找,使用emit将Datatable,DataReader转换成List<T>了.这是从数据库到展示. 但是最近整理Hikari(我写的数据库连接池),发现c#里面数据库 ...
- vue父子组件之间的传值
引入组件 父组件 <div> <form-edit></form-edit> </div> import FormEdit from "路径& ...
- python3爬虫-通过selenium获取TB商品
from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from seleni ...
- BZOJ 3489: A simple rmq problem(K-D Tree)
Time Limit: 40 Sec Memory Limit: 512 MBSubmit: 2579 Solved: 888[Submit][Status][Discuss] Descripti ...
- rpm与yum,at与crontab,sed命令使用
1.简述rpm与yum命令的常见选项,并举例. rpm——软件包管理系统,它使得在Linux下安装.升级.删除软件包的工作变得容易,并且具有查询.验证软件包的功能. 1)安装选项 命令格式: rpm ...
- Python爬虫——Scrapy整合Selenium案例分析(BOSS直聘)
概述 本文主要介绍scrapy架构图.组建.工作流程,以及结合selenium boss直聘爬虫案例分析 架构图 组件 Scrapy 引擎(Engine) 引擎负责控制数据流在系统中所有组件中流动,并 ...