AngularJS之directive

  AngularJS是什么就不多舌了,这里简单介绍下directive。内容基本上是读书笔记,所以如果你看过《AngularJS up and running》,那么这个可以忽略。

  1、在AngularJS中,directives有两个主要的类型:1⃣️UI展示的修改器,如ng-show、ng-model2⃣️可复用的组件,如菜单、轮播器、taps等。
  2、directives定义:

 angular.module('stockMarketApp', []) .directive('stockWidget', [function() {
  return {
3     // Directive definition will go here
}; }]);
  需要注意的是,定义的名字采取驼峰命名,而在HTML中应用应该是-连接。如上面的定义应该为:<div stock-widget></div>
  3、templateUrl,注意的是AngularJS只会在第一次碰到directive的时候去取一次,然后会缓存起来,之后都是从缓存中读取。定义如下:

 angular.module('stockMarketApp') .directive('stockWidget',         [function() {
  return {
3     templateUrl: 'stock.html'
  };
}]);
  4、如果HTML比较小的话,可以直接写行内HTML,放在directive定义的template属性中。
  5、Restrict属性:
The restrict keyword defines how someone using the directive in their code might use it. As mentioned previously, the default way of using directives is via attributes of existing elements (we used <div stock-widget> for ours).
When we create our directive, we have control in deciding how it’s used. The possible values for restrict (and thus the ways in which we can use our directive) are:
A
The letter A in the value for restrict specifies that the directive can be used as an attribute on existing HTML elements (such as <div stock-widget></div>). This is the default value.
E
The letter E in the value for restrict specifies that the directive can be used as a new HTML element (such as <stock-widget></stock-widget>).
C
The letter C in the value for restrict specifies that the directive can be used as a class name in existing HTML elements (such as <div class="stock-widget"> </div>).
M
The letter M in the value for restrict specifies that the directive can be used as HTML comments (such as <!-- directive: stock-widget -→). This was previ‐ ously necessary for directives that needed to encompass multiple elements, like multiple rows in tables, etc. The ng-repeat-start and ng-repeat-end directives were introduced for this sole purpose, so it’s preferable to use them instead of com‐ ment directives.
 
  其中,A是默认的。同时,既可以单独使用,也可以多个一起用。比方说AE,既可以作为属性,也可以作为元素单独用。
  6、link函数,对于directive来说link函数的作用跟controller对于view的作用一样,它定义API和必要的函数。对于每一个directive的实例,AngularJS都会执行其link函数,因此包含其完整的业务逻辑,也不会影响到其它的实例。其定义会传递几个固有的参数,分别为directive元素的$scope、元素本身$element、元素上的属性$attrs,定义如下:
 link: function($scope, $element, $attrs) {}
  其中,完整定义如下:
 angular.module('stockMarketApp') .directive('stockWidget', [function() {
  return {
    templateUrl: 'stock.html',
    restrict: 'AE',
    link: function($scope, $element, $attrs) {
    $scope.getChange = function(stock) {
      return Math.ceil(((stock.price - stock.previous) /
stock.previous) * 100);
};
  } };
}]);
  7、Scope,默认的情况下,directive都继承其父元素的scope,并传递到link函数当中。这会导致一些如下的问题:1⃣️新增的变量和函数会默认修改父元素的scope,其父元素的scope莫名多了属性和方法2⃣️可能会无意覆盖掉父元素scope的函数或者变量3⃣️directive可以隐式的引用父元素的函数或者变量。因此,在定义directive的时候,AngularJS给了我们scope这个key,从而使我们能控制scope,其可用的值如下:
By default, each directive inherits its parent’s scope, which is passed to it in the link function. This can lead to the following problems:
• Adding variables/functions to the scope modifies the parent as well, which suddenly gets access to more variables and functions.
• The directive might unintentionally override an existing function or variable with the same name.
• The directive can implicitly start using variables and functions from the parent. This might cause issues if we start renaming properties in the parent and forget to do it in the directive.
  注意:false是默认的值,即使用父元素传递下来的scope。其中object是最强大的,其不继承父元素的scope,从传统scope的树形中脱离开来,隔离开来,需要directive使用的数据需要父元素在directive引用的时候通过HTML属性传递进来,其传递的值可以分为暗中类别,如下:
false
This is the default value, which basically tells AngularJS that the directive scope is the same as the parent scope, whichever one it is. So the directive gets access to all the variables and functions that are defined on the parent scope, and any modifi‐ cations it makes are immediately reflected in the parent as well.
true
This tells AngularJS that the directive scope inherits the parent scope, but creates a child scope of its own. The directive thus gets access to all the variables and func‐ tions from the parent scope, but any modifications it makes are not available in the parent. This is recommended if we need access to the parent’s functions and infor‐ mation, but need to make local modifications that are specific to the directive.
object
We can also pass an object with keys and values to the scope. This tells AngularJS to create what we call an isolated scope. This scope does not inherit anything from the parent, and any data that the parent scope needs to share with this directive needs to be passed in through HTML attributes. This is the best option when cre‐ ating reusable components that should be independent of how and where they are used.
  8、Replace参数,之前的所有directive在应用到HTML中的时候都会被当做子元素插入进去,可是有的时候我们需要其单独作为一个元素使用,这个时候就可以用到replace参数了。默认设置为false,即作为子元素插入进去。当设置为true的时候,directive的template会替换当前元素,同时旧元素上的属性都会移到新元素上。 
 

AngularJS之directive的更多相关文章

  1. Angularjs之directive指令学习笔记(二)

    1.Directive的五个实例知道driective作用.其中字段restrict.template. replace.transclude.link用法 参考文章链接地址:http://damoq ...

  2. 前端angularJS利用directive实现移动端自定义软键盘的方法

    最近公司项目的需求上要求我们iPad项目上一些需要输入数字的地方用我们自定义的软键盘而不是移动端设备自带的键盘,刚接到需求有点懵,因为之前没有做过,后来理了一下思路发现这东西也就那样.先看一下实现之后 ...

  3. Angularjs的directive封装ztree

    一般我们做web开发都会用到树,恰好ztree为我们提供了多种风格的树插件. 接下来就看看怎么用Angularjs的directive封装ztree <!DOCTYPE html> < ...

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

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

  5. angularJS中directive与directive 之间的通信

    上一篇讲了directive与controller之间的通信:但是我们directive与directive之间的通信呢? 当我们两个directive嵌套使用的时候怎么保证子directive不会被 ...

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

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

  7. AngularJS之Directive,scope,$parse

    AngularJS内幕详解之 Directive AngularJS内幕详解之 Scope AngularJS的指令(Directive) compile和link的区别及使用示例 浅谈Angular ...

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

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

  9. 通过angularjs的directive以及service来实现的列表页加载排序分页

    前两篇:(列表页的动态条件搜索,我是如何做列表页的)分别介绍了我们是如何做后端业务系统数据展示类的列表页以及动态搜索的,那么还剩下最重要的一项:数据展示.数据展示一般包含三部分: 数据列头 数据行 分 ...

随机推荐

  1. linux下包不重新编译php安装openssl 扩展

    今天在刚装好的centos上安装composer,老是报错,提示不安全的,后来发现是由于https的缘故,需要openssl,可能是开始装php的时候没有安装此扩展,网上有好多方案,一开始我认为只有重 ...

  2. CSS实现三角形

    突然想起搞一把三角形. 简单来说: 建一个div  宽度.高度设为0 添加两个样式: border 和 border-color 如果需要一个三角形把其他边框都设为透明 transparent < ...

  3. iOS多线程学习及总结

    能有份网上的存储资料,备以后提升及参考 iOS 多线程编程 简介 一.      iOS有三种多线程编程的技术,分别是: 1.        NSThread 2.        Cocoa NSOp ...

  4. Ubuntu 手工挂载硬盘

    首先我们得到到/dev/sda3这个分区的UUID,使用以下命令: sudo blkid /dev/sda3 结果如下: 然后,我们按照/etc/fstab文件中的格式添加一行如下内容: UUID=9 ...

  5. Unity自动场景保存脚本

    新建一个名为AutoSave的编辑器脚本,并放于Assets/Editor下. using System; using UnityEditor; using UnityEditor.SceneMana ...

  6. 14——小心copying行为

    资源的copying行为决定对象的copying行为. 抑制copying行为,使用引用计数.

  7. 用python+selenium抓取豆瓣读书中最受关注图书并按评分排序

    抓取豆瓣读书中的(http://book.douban.com/)最受关注图书,按照评分排序,并保存至txt文件中,需要抓取书籍的名称,作者,评分,体裁和一句话评 方法一: #coding=utf-8 ...

  8. Lua小技巧

    来公司以后,业务逻辑都用lua写.写了好长时间了,到最近才觉得有点掌握了Lua的灵活.最近用Lua写了个类似集合一样的东西,如果两次向集合里放入同一个元素,就会报错,方便检查配置.代码如下: -- k ...

  9. redis-cluster 迁移过程错误记录

    因为集群内的 单点redis消耗 内存达到了14个G,所以需要增加新的节点,并将数据迁移过去,使用 redis-trib reshard ip:port A : 2105slot       14.5 ...

  10. 更新系统没有mac dashboard 问题解决

    今天更新了mac的系统到EL Capitan,结果出来以后发现平时经常使用的mac dashboard没了,就是这玩意: 找了半天方法,终于知道了这个叫“dashboard” (md之前完全不知道,无 ...