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. 使用Robomongo连接数据库不成功:没有启动MongoDB

    启动MongoDB ➜ ~ mongod --07T16:: I CONTROL [initandlisten] MongoDB starting : pid= port= dbpath=/data/ ...

  2. Linux PHP7的openssl扩展安装

    Linux环境下使用PHPmailer发送邮件时,出现如下错误: SMTP -> ERROR: Failed to connect to server: Unable to find the s ...

  3. Spark2.0机器学习系列之4:Logistic回归及Binary分类(二分问题)结果评估

    参数设置 α: 梯度上升算法迭代时候权重更新公式中包含 α :  http://blog.csdn.net/lu597203933/article/details/38468303 为了更好理解 α和 ...

  4. PLSQLDeveloper安装与配置

    1.前提:首先要有oracle数据库或者有oracle服务器,才可以实现使用PLSQL Developer 工具连接到oracle数据库进行开发 2.下载PLSQLDeveloper并解压 3.配置环 ...

  5. 关于safenetde 的明文 密文 数据 。这个数组使用 safenet的助手 产生的。

    关于safenetde 的明文 密文  数据  .这个数组使用 safenet的助手 产生的. 下图是生成的数组 例如: { 0x9B, 0xFD, 0xF5, 0xA6, 0xF5, 0x57, 0 ...

  6. CDOJ 1287 MC挖矿世界(Spfa+set优化)

    题目大意:原题链接 解题思路:此题要求多点最短距离,但是直接套用floyd会超时. 然后我们想直接从每一个点开始bfs就好了,但是还是会TLE,为什么呢? 因为你访问了很多次没有意义的地方,因为有些点 ...

  7. 机器学习实战python3 决策树ID3

    代码及数据:https://github.com/zle1992/MachineLearningInAction 决策树 优点:计算复杂度不高,输出结果易于理解,对中间值的缺失不敏感,可以处理不相关特 ...

  8. matlab和mathematics最新的FTP地址

    https://dio.obspm.fr/interne/logiciels/matlab/ 分享一个地址,非常好的FTP网站.

  9. 资料:mnist.pkl.gz数据包的下载以及数据内容解释

    deeplearning.net/data/mnist/mnist.pkl.gz The MNIST dataset consists of handwritten digit images and ...

  10. Ubuntu&Linux系统出现文件系统只读Read-only file system 的快速解决方法

    问题描述: 周末运行盘平台服务程序,周一来操作系统卡顿,主进程已退出,重启进程时提示Read-only file system:新建目录和其他chmod -R等等操作都提示Read-only file ...