E 表示该指令是一个element; A 表示该指令是attribute; C 表示该指令是class; M 表示该指令是注视

实例如下:

原帖:www.thinkster.io/angularjs/rep5re7gTM/angularjs-directive-restrictions

While it’s cool to make a custom element like we did the the previous cast, it’s actually more common to do things like create custom attributes. These attributes are going to add things like behaviors, and we can do so by using restrict “A”. “A” is for attribute, “E” is for element. You can then provide a linking function, which is where you will put whatever the behavior is. We’re just going to alert “I’m working” to the user.

main.js
  1. var app = angular.module("superhero", [])
  2. app.directive("superman", function(){
  3. return {
  4. restrict: "A",
  5. link: function(){
  6. alert("I'm working");
  7. }
  8. };
  9. });

From here, instead of having superman as an element, let’s do a div with superman as an attribute:

index.html
  1. <div ng-app="superhero">
  2. <div superman></div>
  3. </div>

Now if we refresh, you’ll see the alert saying “I’m working” Another restriction we can use is “C” for class. If we change restrict to “C” and refresh without changing anything, we can see that nothing happens. We need to change the directive from an attribute to a class of the div.

index.html
  1. <div ng-app="superhero">
  2. <div class="superman"></div>
  3. </div>

If we refresh now, we’ll see “I’m working” alerted again. The last restriction is “M” for comment. If we change restrict to “M” and create a comment starting with “directive:” and then the name of our directive, refresh, and we’ll see that it works again.

index.html
  1. <div ng-app="superhero">
  2. <!-- directive:superman -->
  3. </div>

The “M” restriction is used the least often, usually only for backwards compatibility and for passing markup validations. Typically it’s best to add behaviors in attributes so you can stack them.

We’ll create another attribute directive, call it “flash” and set the linking function to alert “I’m working faster” and change “superman” to alert “I’m working stronger” (Don’t forget to change the “superman” directive’s restriction back to “A”)

main.js
  1. var app = angular.module("superhero", [])
  2. app.directive("superman", function(){
  3. return {
  4. restrict: "A",
  5. link: function(){
  6. alert("I'm working");
  7. }
  8. };
  9. });
  10. app.directive("flash", function(){
  11. return {
  12. restrict: "A",
  13. link: function(){
  14. alert("I'm working");
  15. }
  16. };
  17. });

Now we should have a div with both “superman” and “flash” as attributes

index.html
  1. <div ng-app="superhero">
  2. <div superman flash></div>
  3. </div>

If we refresh, we’ll see “I’m working stronger” and then “I’m working faster”

To recap: “E” is for element, “A” is for attribute, “C” is for class, and “M” is for comment. Attributes are going to be the main ones as far as adding behaviors that get used the most. If you don’t specify the restrict property it will default to “A”

angular directive restrict 的用法的更多相关文章

  1. angular directive scope

    angular directive scope 1.当directive 中不指定scope属性,则该directive 直接使用 app 的scope: 2.当directive 中指定scope属 ...

  2. angular directive 的controllerAs的用法

    原文: https://stackoverflow.com/questions/31857735/using-controlleras-with-a-directive --------------- ...

  3. angular directive

    1.restrict (字符串)可选参数,指明指令在DOM里面以什么形式被声明: 取值有:E(元素),A(属性),C(类),M(注释),其中默认值为A: E(元素):<directiveName ...

  4. angular directive自定义指令

    先来看一下自定义指令的写法 app.directive('', ['', function(){ // Runs during compile return { // name: '', // pri ...

  5. angular directive指令内的参数

    angular.module('myApp', []) .directive('myDirective', function() { return { restrict: String, priori ...

  6. angular directive指令的复用

    “指令之之所以要定义成指令就是为了复用!” 指令一定是可以用在不同的controller里面的,为了在不同的controller去使用它,我们一定要给指定的配置项一个指令.这样才能跟外面的控制器进行交 ...

  7. 使用 angular directive 和 json 数据 D3 随着标签 donut chart演示样本

    使用angular resource载入中priorityData.json中间json数据,结合D3绘制甜甜圈图.执行index.html其结果见于图.: priorityData.json中jso ...

  8. angular directive知识

    一般来讲 directive名字遵循一下规则: 1.忽略以x-和data-为元素/属性的前缀 2.转化“:”,“-”,“_”命名为驼峰命名 如下所示 <div ng-controller=&qu ...

  9. Angular @HostBinding()和@HostListener()用法

    @HostBinding()和@HostListener()在自定义指令时非常有用.@HostBinding()可以为指令的宿主元素添加类.样式.属性等,而@HostListener()可以监听宿主元 ...

随机推荐

  1. 20165324 《Java程序设计》第九周学习总结

    学号 20165324 <Java程序设计>第九周学习总结 教材学习内容总结 第十三章 Java网络编程 URL类 使用URL创建对象的应用程序称为客户端 一个URL对象封装一个具体资源的 ...

  2. string与CString对比

    string是标准C++库中的字符串类,CString是在Windows开发环境下常用的字符串类,CString目前已从MFC中分离出来可以单独使用,只需包含atlstr.h即可. 相比string, ...

  3. Java基础知识陷阱(八)

    本文发表于本人博客. 这次我来说说关于&跟&&的区别,大家都知道&是位运算符,而&&是逻辑运算符,看下面代码: public static void m ...

  4. 2018秦皇岛ccpc-camp Steins;Gate (原根+FFT)

    因为给定的模数P保证是素数,所以P一定有原根. 根据原根的性质,若\(g\)是\(P\)的原根,则\(g^k\)能够生成\([1,P-1]\)中所有的数,这样的k一共有P-2个. 则\(a_i*a_j ...

  5. myeclipse自动生成相应对象接收返回值的快捷键

    在你要自动生成返回值对象的那一行的末尾(注意一定要将光标点到最后),按Alt+Shift+L:就可以了.

  6. ThinkPHP 调用后台方法

    <a href="__URL__/del/id/{$vo['id']}">删除</a>

  7. Nginx访问控制_IP访问控制(http_access_module)原理、局限性、解决方法讲解

    基于IP的访问控制,基于Nginx的http_access_module模块,是Nginx本身内置的模块,不需要安装的时候配置.也就是允许哪些IP访问,不允许哪些IP访问 server { liste ...

  8. addEventListener、onclick和jquery的bind()、click()

    addEventListener("click",function(event){},false); removeEventListener("click",f ...

  9. [Linux 004]——用户和用户组以及 Linux 权限管理(二)

    到权限了.前面讲到了 Linux 中的用户和用户主管理,其实它们的本质(或者用户和用户组出现的初衷)都是方便权限管理.权限管理对于计算机的重要性不言而喻,权限让每个用户能够安安心心的使用计算机,而不用 ...

  10. 20145321 《Java程序设计》第8周学习总结

    20145321 <Java程序设计>第8周学习总结 教材学习内容总结 第十五章 时间与日期 15.1 日志 1.使用日志的起点是Logger类,要取得Logger类,必须使用Logger ...