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. RabbitMQ、Memcache、Redis RabbitMQ

    RabbitMQ 解释RabbitMQ,就不得不提到AMQP(Advanced Message Queuing Protocol)协议. AMQP协议是一种基于网络的消息传输协议,它能够在应用或组织之 ...

  2. Python3 计算城市距离

    利用上一篇得到的城市经纬度算城市距离 import requests from math import radians, cos, sin, asin, sqrt def geocode(addres ...

  3. 使用CXF+Spring发布WebService,启动报错

    使用CXF+Spring发布WebService,启动报错,日志如下: 五月 12, 2017 9:01:37 下午 org.apache.tomcat.util.digester.SetProper ...

  4. C++ 第三十四天

    c++ 已经搁了很久了,之所以捡起来是因为学校数据结构课程设置 **.我对 c++ 的掌握非常不扎实,因为除了顺序阅读 c++ primer 外就没有什么实践, 但是我又无法忍受自己写出来的 * 一样 ...

  5. composer错误_Content-Length mismatch, received 84697 bytes out of the expected..

    使用composer下载源码出现错误 [Composer\Downloader\TransportException] Content-Length mismatch, received bytes ...

  6. Cisco 路由交换 常用查询语句

    基本信息查询语句 #查看全配置信息 #show running-configure #查看vlan信息 #show vlan brief #查看物理直连信息 #show cdp neighbors d ...

  7. Ant Design 常用命令汇总

    Ant Design React 安装 1. 安装脚手架工具# antd-init 是一个用于演示 antd 如何使用的脚手架工具,真实项目建议使用 dva-cli. $ npm install an ...

  8. z-albert之开启博文之路

    其实注册博客园已经蛮久的了,一直都只是停留在看,却没有自己动手一篇属于自己的技术博文.之所以以前一直没写,以前没有工作,一直都是小白.然而今天为什么感写了呢,并不是自己比以前懂得多多少,而是希望将自己 ...

  9. SaltStack使用salt-ssh模式-第十一篇

    salt-ssh介绍 1.salt-ssh 是 0.17.0 新引入的一个功能,不需要minion对客户端进行管理,也不需要master. 2.salt-ssh 支持salt大部分的功能:如 grai ...

  10. JDK 中的监控与故障处理工具-02 (jps)

    jps : JVM Process Status Tool jps 命令可以列出正在运行的虚拟机进程, 并显示虚拟机执行的 main class 的名称(main函数所在的类),以及这些进程的本地虚拟 ...