AngularJS之directive

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

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

  2、directives定义:

1 angular.module(‘stockMarketApp‘, []) .directive(‘stockWidget‘, [function() {
2   return {
3     // Directive definition will go here
4 }; }]);

  需要注意的是,定义的名字采取驼峰命名,而在HTML中应用应该是-连接。如上面的定义应该为:<div stock-widget></div>

  3、templateUrl,注意的是AngularJS只会在第一次碰到directive的时候去取一次,然后会缓存起来,之后都是从缓存中读取。定义如下:

1 angular.module(‘stockMarketApp‘) .directive(‘stockWidget‘,         [function() {
2   return {
3     templateUrl: ‘stock.html‘
4   };
5 }]);

  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,定义如下:

1 link: function($scope, $element, $attrs) {}

  其中,完整定义如下:

 1 angular.module(‘stockMarketApp‘) .directive(‘stockWidget‘, [function() {
 2   return {
 3     templateUrl: ‘stock.html‘,
 4     restrict: ‘AE‘,
 5     link: function($scope, $element, $attrs) {
 6     $scope.getChange = function(stock) {
 7       return Math.ceil(((stock.price - stock.previous) /
 8                   stock.previous) * 100);
 9            };
10   } };
11 }]);

  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会替换当前元素,同时旧元素上的属性都会移到新元素上。

时间: 2024-08-05 23:41:08

AngularJS之directive的相关文章

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

最近公司项目的需求上要求我们iPad项目上一些需要输入数字的地方用我们自定义的软键盘而不是移动端设备自带的键盘,刚接到需求有点懵,因为之前没有做过,后来理了一下思路发现这东西也就那样.先看一下实现之后的效果: 实现的效果就是当点击页面中需要弹出软键盘的时候软键盘弹出,浮在页面的中间,和模态框一样的效果,可以在软键盘中输入任何数字,附带的功能有小数点.退格.清空.确定等功能.当在键盘上点击数字的时候页面中的表单中实时的添加对应的数字,上图中可以看到. 产品经理那边给的原因是iPad屏幕本来就小,如

angularjs中directive声明scope对象时修饰符解释和用法

在angular中我们定义directive方法时,可以看到 return { restrict: 'AE', scope: {}, template: '<div></div>', link: function() {} } 除了代码中出现的属性,还有一些其他的属性可供配置,这里不作详述. 今天我们要说的重点是scope字段. 常规用法设置 scope: { name: '=', age: '=', sex: '@', say: '&' } 假设我们的hml代码如下 &l

深究AngularJS(4)——Directive和Scope数据隔离与交互

什么是隔离 Scope AngularJS 的 directive 默认能共享父 scope 中定义的属性,例如在模版中直接使用父 scope 中的对象和属性.通常使用这种直接共享的方式可以实现一些简单的 directive 功能.当你需要创建一个可重复使用的 directive,只是偶尔需要访问或者修改父 scope 的数据,就需要使用隔离 scope.当使用隔离 scope 的时候,directive 会创建一个没有依赖父 scope 的 scope,并提供一些访问父 scope 的方式.

AngularJS 利用directive集成JQuery ZTree

前段时间一直在看AngularJS的资料,感觉是个很好的框架,很想有机会尝试用它做点什么. JQuery ZTree是国内非常不错的JQuery插件,功能齐全,文档和API也非常的友好,之前项目上常用此插件. AngularJS功能虽然非常强大,但UI上提供的插件不像JQuery那么多,而且只能通过directive定义扩展的UI插件,虽然国外已经提供了一些基于directive的Tree功能实现,但毕竟不像ZTree那样强大,而且Tree是做项目中很长用的一个基本功能. 因此,花了一点时间做了

AngularJs 指令directive之require

controller的用法分为两种情形,一种是require自定义的controller,由于自定义controller中的属性方法都由自己编 写,使用起来比较简单:另一种方法则是require AngularJS内建的指令,其中大部分时间需要require的都是ngModel这个指令. 在自定义Angular指令时,其中有一个叫做require的字段,这个字段的作用是用于指令之间的相互交流.举个简单的例子,假如我们现在需要编写两 个指令,在linking函数中有很多重合的方法,为了避免重复自己

AngularJS clone directive 指令复制

需求背景: directive模块化某表单信息,但表单信息可加入多条.此时就涉及到clone directive. 解决方式: 能够通过使用angularjs中$compile来进行clone directive.clone direcitve中常涉及到数据的绑定. 详细方法: tw.factory('DirectiveUtil', [function() { var DirectiveUtil = {}; DirectiveUtil.DirectiveBuilder = function(di

angularJS指令系统---Directive

指令:Directive angularJS 有一套完整的,可拓展的,用来帮助web应用开发的指令集: 在建立DOM期间,和HTML关联着的指令会被检测到,并被执行: 在angularJS中将前缀为 ng- 这种属性称之为指令,其作用就是为DOM元素调用方法,定义行为绑定数据等: 简单说:当一个angular 应用启动时,angular会遍历DOM树来解析HTML,根据指令不同,完成不同的操作: 指令属性小提示: ng-xxx 的属性并不是标准中定义的属性,很多情况下语法校验是无法通过的: HT

angularJS中directive与controller之间的通信

当我们在angularJS中自定义了directive之后需要和controller进行通讯的时候,是怎么样进行通讯呢? 这里介绍3种angular自定义directive与controller通信的指令. 1.指令作用域中的"@" 作用:把当前属性作为字符串传递实现指令与html页面元素关联. 1 <!DOCTYPE html> 2 <html ng-app="demoapp"> 3 <head lang="en"

AngularJS自定义Directive

什么时候需要自定义Directive? 1. 使你的Html更具语义化,不需要深入研究代码和逻辑即可知道页面的大致逻辑. 2. 抽象一个自定义组件,在其他地方进行重用. 看一下如下2个代码片段: 示例1: 1 <body> 2 <div> 3 <p>This is your class name.</p> 4 <div> 5 <p>Your teacher:</p> 6 <p>Mr. Wang</p>