指令 Directive

指令要点

大漠老师的教学节点

  • 解析最简单的指令 hello: 匹配模式 restrict
  • 解析最简单的指令 hello: templatetempmlateUrl$templateCache
  • 解析最简单的指令 hello: replacetransclude
  • compilelink (操作元素、添加 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')
}
});

compilelink

  • 加载阶段

    • 加载 angular.js,找到 ng-app 指令,确定应用的边界
  • 编译阶段

    • 遍历 DOM,找到所有指令
    • 根据指令代码中的 templatereplacetransclude 转换 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 &amp; Speed</superman>
<superman strength speed light>Stength &amp; Speed &amp; 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 学习笔记的更多相关文章

  1. AngularJS的学习笔记(一)

    声明:单纯作为我自己的学习笔记,纯是为了自己学习,上面的话都是从各处粘贴,如有冒犯,请原谅我这个小菜鸟~ AngularJS使用了不同的方法,它尝试去补足HTML本身在构建应用方面的缺陷. 使用双大括 ...

  2. angularJS Directive学习

    Directive 指令 直接上实例 index.html <!doctype html> <html ng-app="drag"> <head> ...

  3. AngularJs初步学习笔记(part1)

    一.摘要: angular是采用JavaScript编写的前端mvc框架,帮助开发者编写现代化的单页面应用.它尤其适用编写有大量CRUD操作的,具有Ajax风格的客户端应用. 二.总结: Angula ...

  4. angularjs directive学习心得

    一些常见的错误 在angularjs里,创建directive时,directive的名称应该要使用驼峰式,例如myDirective,而在html里要调用它的时候,就不能用驼峰式了,可以用my-di ...

  5. 【angularJS】学习笔记

    一.一个html中多个ng-app //对于ng-app初始化一个AngularJS程序属性的使用需要注意,在一个页面中AngularJS自动加载第一个ng-app,其他ng-app会忽略 //如果需 ...

  6. AngularJS 系列 学习笔记 目录篇

    目录: AngularJS 系列 01 - HelloWorld和数据绑定 AngularJS 系列 02 - 模块 (持续更新)

  7. AngularJS的学习笔记(二)

    只给自己看的. AngularJS 表达式 angularjs 使用表达式将数据绑定到html中. AngularJS 表达式写在双大括号内:{{ expression }}. AngularJS 表 ...

  8. day65—angularJS的学习笔记1

    转行学开发,代码100天—2018-05-20 AngularJS的引用示例: <!DOCTYPE html> <html> <head> <title> ...

  9. AngularJs学习笔记--directive

    原版地址:http://code.angularjs.org/1.0.2/docs/guide/directive Directive是教HTML玩一些新把戏的途径.在DOM编译期间,directiv ...

随机推荐

  1. LINUX常用命令--重定向、管道篇(四)

    一.Linux重定向 重定向能够实现Linux命令的输入输出与文件之间重定向,以及实现将多个命令组合起来实现更加强大的命令.这部分涉及到的比较多的命令主要有: 涉及到的比较多的命令主要有: cat:连 ...

  2. android ViewHolder 使用

    android中使用ListView   ExpandableListView  数据适配器adapter很多都是自己定义,自己定义数据适配器时,要重写getView.重写getView为了不让每次调 ...

  3. Fix Some bytes have been replaced with the Unicode substitution character while loading file XXX.cs with Chinese Simplified (GB2312) encoding

    When we use <strong>visual studio</strong> open source file or any other file, we may en ...

  4. SSIS: 把存储在数据库中的图片导出来

    Data Flow Task Step 1 获取二进制图片数据 )='C:\labs\Images\' SELECT ThumbNailPhoto,@path+ThumbnailPhotoFileNa ...

  5. powerdesigner反向MySQL5.1数据库 生成ER图

    我用的powerdesigner是15.1版本,数据库是MySQL5.1.57 (1)首先新建一个“PhysicalDataModel”类型的文件,然后点击“Database”->"C ...

  6. java 获取本机ip及mac地址

    package com.achun.test; import java.net.Inet4Address;import java.net.Inet6Address;import java.net.In ...

  7. jvm栈和堆详解

    Java把内存分成两种,一种叫做栈内存,一种叫做堆内存 在函数中定义的一些基本类型的变量和对象的引用变量都是在函数的栈内存中分配.当在一段代码块中定义一个变量时,java就在栈中为这个变量分配内存空间 ...

  8. C和C++运算符 (转)

    这里是C和C++语言的运算符列表.所有列出的运算符皆含纳于C++:第三个栏目里的内容也使用C来描述.应当注意的是C不支持运算符重载. 下列运算符在两个语言中都是顺序点(运算符未重载时): && ...

  9. SSH框架的简化

    ---恢复内容开始--- 一.简易化的SSH框架,如图: SSH框架的搭建,我就不多说了. 二.简易的ssh框架的步骤: 1.重新编写applicationContext.xmlwen文件 <一 ...

  10. C++学习之函数指针

     C++学习之函数指针          和数据项类似,函数也有地址,函数的地址是存储在机器语言代码的内存的开始地址.通常,这些地址对用户而言,不重要也没什么用处,但对程序而言,它却很有用. 一.函数 ...