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. String,StringBuffer和StringBuilder的区别

    面试的时候经常问到的一个题:这里先说明下三者在JVM中的执行速度:StringBuilder > StringBuffer > String,原因且看下面慢慢阐述. 首先看一个概念,为啥一 ...

  2. 练习webpack遇到的一些问题以及解决办法,供自己以后参考

    1.利用nodeJs安装webpack时报出以下错误提示: 这个错误我在网上搜了一下,说是npm文件配置问题,也就是权限不够 解决办法:npm config set registry http://r ...

  3. selenium 代理 Cookies 截图 等待 调用JS

    改变用户代理 读取Cookies 调用Java Script Webdriver截图 页面等待 1. 改变用户代理 import org.junit.AfterClass; import org.ju ...

  4. windows 2003 iis php

    我的环境 是   windows server200 ee   iis6.0  程序是php 1.一台安装好的 Windows 2003 服务器,并且已经安装了 IIS 6. 2.下载 windows ...

  5. C++匿名名字空间

    转自:http://blog.csdn.net/eric_arrow/article/details/8978905 名字空间(namespace),是C++提供的一个解决符合名字冲突的特性.标准规定 ...

  6. struts.xml 配置文件的主要元素

    1.package元素 作用: 在struts2的配置文件中引入了面向对象思想.分包管理,易于管理动作类,便于模块化开发动作类. 属性: name:包的名称.名称唯一. extends:一般情况下需要 ...

  7. Java语法糖设计

    语法糖 Java语法糖系列,所以首先讲讲什么是语法糖.语法糖是一种几乎每种语言或多或少都提供过的一些方便程序员开发代码的语法,它只是编译器实现的一些小把戏罢了,编译期间以特定的字节码或者特定的方式对这 ...

  8. Java中的JDBC是什么?

    JDBC(Java Data Base Connectivity, Java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它有一组用Java语言编写的类和接 ...

  9. Linux使用free命令查看实际内存占用

    转自:http://www.cnblogs.com/pengdonglin137/p/3315124.html Linux下在终端环境下可以使用free命令看到系统实际使用内存的情况,一般用free ...

  10. NetBeans 启动时出现 Invalid jdkhome specified提示

    执行 NetBeans 出现如下文字内容: Invalid jdkhome specifiedCannot locate java installation in specifired jdkhome ...