AngularJS Directive 学习笔记
指令 Directive
指令要点
大漠老师的教学节点
- 解析最简单的指令 hello: 匹配模式 restrict
- 解析最简单的指令 hello: template、tempmlateUrl、$templateCache
- 解析最简单的指令 hello: replace 与 transclude
- compile 与 link (操作元素、添加 CSS 样式、绑定事件)
- 指令与控制器之间的交互
- 指令间的交互
- scope 的类型与独立 scope
- scope 的绑定策略
简单的指令
app.directive('hello', function() {
return {
// 四种: Element, Attribute, Comment, Class
// Default: Attribute
// Comment: <!-- directive:hello -->
restrict: "E/A/M/C",
// 模板
template: '<h1>Hi everyone! </h1>',
templateUrl: 'temp.html',
// 是否替换节点内的内容
replace: true
}
});
transculde 转置
transclude 作用在于转置指令内部原有的内容,以免 replace: true 被设置时内部内容被全部替换,这样非常有用于嵌套指令
app.directive('ele', function() {
return {
// 注意:不能用 replace: true
restrict: 'AE',
transclude: true,
// 标签内部内容将会被转置到 div[ng-transclude] 内部
template: "<h1>Hello AngularJS!</h1><div ng-transclude></div>"
}
});
模板缓存
run 方法会在注册器加载完所有模块之后被执行一次
$templateCache 可以缓存模板以供多个指令使用
put & get 类似面向对象的 setter & getter 方法
app.run(function($templateCache) {
$templateCache.put('hello.html', '<div>Hello AngularJS!</div>');
});
// use get method to get cache
app.directive('ele', function($templateCache) {
return {
template: $templateCache.get('hello.html')
}
});
compile 与 link
加载阶段
- 加载 angular.js,找到 ng-app 指令,确定应用的边界
编译阶段
- 遍历 DOM,找到所有指令
- 根据指令代码中的 template、replace、transclude 转换 DOM 结构
- 如果存在 compile 函数则调用
链接阶段
- 对每一条指令运行 link 函数
- link 函数一般用来操作 DOM、绑定事件监听器
指令调用控制器的方法,使用 link
HTML 代码
<loader howToLoad="loadData()">Hover to load</loader>
<loader howToLoad="loadData2()">Hover to load</loader>
AngularJS 代码
myModule.controller('MyCtrl', ['$scope',
function($scope) {
$scope.loadData = function() {
console.log('Loading...');
};
$scope.loadData2 = function() {
console.log('Loading2...');
}
}
]);
myModule.directive('loader', function() {
return {
resetrict: 'AE',
template: '',
replace: true,
link: function(scope, element, attr) {
// 绑定事件
element.bind('mouseenter', function() {
// 以下两种形式都可以,推荐下面的
scope.loadData();
scope.$apply('loadData()');
// 获取属性值
// 根据指令特定属性的不同应用不同方法
// 方法应小写
scope.$apply(attrs.howtoload);
});
}
}
});
指令之间的交互
重点是创建独立 scope,使得指令之间不互相影响
HTML 代码
<superman strength>Strength</superman>
<superman strength speed>Strength & Speed</superman>
<superman strength speed light>Stength & Speed & Light</superman>
AngularJS 代码
myModule.directive('superman', function() {
return {
// 创建独立 scope
scope: {},
restrict: 'AE',
// 希望指令暴露出一些方法编写在 controller 里面供其他指令调用
// 同时使用 this 指代 $scope,这样交互的指令才能引用
controller: function($scope) {
$scope.abilities = [];
this.addStrength = function () {
$scope.abilities.push('Strength');
};
this.addSpeed = function () {
$scope.abilities.push('Speed');
};
this.addLight = function () {
$scope.abilities.push('Light');
};
},
// link 处理指令内部事件
link: function (scope, element, attrs) {
element.addClass('btn btn-primary btn-lg');
element.bind('mouseenter', function() {
console.log(scope.abilities);
});
}
};
});
myModule.directive('strength', function() {
return {
// 依赖于 superman 指令,这样 link 函数才可以调用 supermanCtrl 参数
require: '^superman',
link: function(scope, element, attrs, supermanCtrl) {
supermanCtrl.addStrength();
}
};
});
myModule.directive('speed', function() {
return {
require: '^superman',
link: function(scope, element, attrs, supermanCtrl) {
supermanCtrl.addSpeed();
}
};
});
myModule.directive('light', function() {
return {
require: '^superman',
link: function(scope, element, attrs, supermanCtrl) {
supermanCtrl.addLight();
}
};
});
AngularJS Directive 学习笔记的更多相关文章
- AngularJS的学习笔记(一)
声明:单纯作为我自己的学习笔记,纯是为了自己学习,上面的话都是从各处粘贴,如有冒犯,请原谅我这个小菜鸟~ AngularJS使用了不同的方法,它尝试去补足HTML本身在构建应用方面的缺陷. 使用双大括 ...
- angularJS Directive学习
Directive 指令 直接上实例 index.html <!doctype html> <html ng-app="drag"> <head> ...
- AngularJs初步学习笔记(part1)
一.摘要: angular是采用JavaScript编写的前端mvc框架,帮助开发者编写现代化的单页面应用.它尤其适用编写有大量CRUD操作的,具有Ajax风格的客户端应用. 二.总结: Angula ...
- angularjs directive学习心得
一些常见的错误 在angularjs里,创建directive时,directive的名称应该要使用驼峰式,例如myDirective,而在html里要调用它的时候,就不能用驼峰式了,可以用my-di ...
- 【angularJS】学习笔记
一.一个html中多个ng-app //对于ng-app初始化一个AngularJS程序属性的使用需要注意,在一个页面中AngularJS自动加载第一个ng-app,其他ng-app会忽略 //如果需 ...
- AngularJS 系列 学习笔记 目录篇
目录: AngularJS 系列 01 - HelloWorld和数据绑定 AngularJS 系列 02 - 模块 (持续更新)
- AngularJS的学习笔记(二)
只给自己看的. AngularJS 表达式 angularjs 使用表达式将数据绑定到html中. AngularJS 表达式写在双大括号内:{{ expression }}. AngularJS 表 ...
- day65—angularJS的学习笔记1
转行学开发,代码100天—2018-05-20 AngularJS的引用示例: <!DOCTYPE html> <html> <head> <title> ...
- AngularJs学习笔记--directive
原版地址:http://code.angularjs.org/1.0.2/docs/guide/directive Directive是教HTML玩一些新把戏的途径.在DOM编译期间,directiv ...
随机推荐
- [Leetcode][Python]30: Substring with Concatenation of All Words
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 30: Substring with Concatenation of All ...
- python中判断readline读到文件末尾
fp = open('somefile.txt') while True: line = fp.readline() if not line: #等价于if line == "": ...
- Brew install for mac
安装命令例如以下: curl -LsSf http://github.com/mxcl/homebrew/tarball/master | sudo tar xvz -C/usr/local --st ...
- 今天,Java编程周末提高班(第一期)正式结束
Java编程周末提高班(第一期),走过了近两个月历程,一共同拥有68人次学生周末到老师家进行Java学习与交流.近距离的和一群年轻的学习接触,收获非常多,特别是对以后教学的改进.在学习的闲暇.大家自己 ...
- 三维CAD塑造——基于所述基本数据结构一半欧拉操作模型
三维CAD塑造--基于所述基本数据结构一半欧拉操作模型(elar, B_REP) (欧拉操作 三维CAD建模课程 三维CAD塑造 高曙明老师 渲染框架 brep 带洞 带柄 B_REP brep ...
- redis持久化与可用性
redis对于持久化有快照及aof日志文件两种形式. 快照db文件,长处是二进制,大小比aof日志文件小.但会丢失最后一次成功备份时间到down机时间的数据. aof相比而言文件大小就大了点,但相对快 ...
- CodeForces Round #179 (295A) - Greg and Array 一个线段树做两次用
线段树的区间更新与区间求和...一颗这样的线段树用两次... 先扫描1~k...用线段树统计出每个操作执行的次数... 那么每个操作就变成了 op. l , op.r , op.c= times* ...
- javascript中this指针的认识
javascript中上下文环境就是this指针,即被调用函数所处的环境.这个上下文环境在大多数情况下指的是函数运行时封装这个函数的那个对象:当不通过任何对象单独调用一个函数时,上下文环境指的就是全局 ...
- 浅谈SpringMVC(一)
一.SpringMVC引言 Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面.Spring 框架提供了构建 Web 应用程序的全功能 MV ...
- HDU 4498 Function Curve (分段, simpson)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 最近太逗了...感觉成都要打铁了...只能给队友端 ...