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.
var app = angular.module("superhero",[])
app.directive("superman",function(){
return{
restrict:"A",
link:function(){
alert("I'm working");
}
};
});
From here, instead of having superman as an element, let’s do a div with superman as an attribute:
<div ng-app="superhero">
<div superman></div>
</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.
<div ng-app="superhero"> <div superman></div> </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.
<div ng-app="superhero">
<!-- directive:superman -->
</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”)
var app = angular.module("superhero",[])
app.directive("superman",function(){
return{
restrict:"A",
link:function(){
alert("I'm working");
}
};
});
app.directive("flash",function(){
return{
restrict:"A",
link:function(){
alert("I'm working");
}
};
});
Now we should have a div with both “superman” and “flash” as attributes
<div ng-app="superhero">
<div superman flash></div>
</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”
AngularJS - Directive Restrictions的更多相关文章
- 学习AngularJs:Directive指令用法
跟我学AngularJs:Directive指令用法解读(上) http://blog.csdn.net/evankaka/article/details/51232895 跟我学AngularJs: ...
- angularjs directive 实例 详解
前面提到了angularjs的factory,service,provider,这个可以理解成php的model,这种model是不带html的,今天所说的directive,也可以理解成php的mo ...
- 学习AngularJs:Directive指令用法(完整版)
这篇文章主要学习AngularJs:Directive指令用法,内容很全面,感兴趣的小伙伴们可以参考一下 本教程使用AngularJs版本:1.5.3 AngularJs GitHub: http ...
- Angularjs directive全面解读(1.4.5)
说到Angularjs directive即指令,可以这么说Angularjs的灵魂就是指令,学会Angularjs指令那么你的Angularjs的武功就修炼了一半了,当然这只是鄙人的一点点独到见解, ...
- angularjs directive (自定义标签解析)
angularjs directive (自定义标签解析) 定义tpl <!-- 注意要有根标签 --> <div class="list list-inset" ...
- 步入angularjs directive(指令)--点击按钮加入loading状态
今天我终于鼓起勇气写自己的博客了,激动与害怕并存,希望大家能多多批评指导,如果能够帮助大家,也希望大家点个赞!! 用angularjs 工作也有段时间了,总体感觉最有挑战性的还是指令,因为没有指令的a ...
- AngularJS Directive 隔离 Scope 数据交互
什么是隔离 Scope AngularJS 的 directive 默认能共享父 scope 中定义的属性,例如在模版中直接使用父 scope 中的对象和属性.通常使用这种直接共享的方式可以实现一些简 ...
- [译]angularjs directive design made easy
原文: http://seanhess.github.io/2013/10/14/angularjs-directive-design.html AngularJS directives很酷 Angu ...
- AngularJS directive 指令相关记录
.... .directive('scopeDemo',function(){ return{ template: "<div class='panel-body'>Name: ...
随机推荐
- JMeter PerfMon Metrics Collector性能监控插件
官方文档地址https://jmeter-plugins.org/wiki/PerfMon/ JMeter是一款压力测试工具,我们也可以用它来监控服务器资源使用情况. JMeter正常自带可以通过To ...
- OO第二单元の小结
第二单元(线程与电梯问题)总结博客 三次作业的设计策略 第一次:本次作业只有一部电梯,而且不用捎带.因此,我一共设计了两个线程:一个负责管理输入,一个负责电梯运行.同时,我将调度队列设置为单例模式,里 ...
- XMPPManager 解析
一.用户登录流程 用户登录流程.png 注意:XMPP核心文件,基于TCP的XML流的传输,XMPPFrame框架是通过代理的方式实现消息传递的 实现用户登录的步骤如下: 1.实例化XMPPStrea ...
- Eclipse安装genymotion最新的方法
https://www.cnblogs.com/WXBai/p/5938884.html 安卓开发: http://tools.android-studio.org/index.php/sdkhttp ...
- Java将\替换成/
public static void main(String[] args) { String str="upload\\media\\201904\\i4Qjz8E40xGQovUq-2C ...
- UITableView 头部效果/放大/移动跟随效果
[self.tableView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOp ...
- springboot(十三)-分库分表-手动配置
sharding-jdbc简介 Sharding-JDBC直接封装JDBC API,可以理解为增强版的JDBC驱动,旧代码迁移成本几乎为零: 可适用于任何基于java的ORM框架,如:JPA, Hib ...
- SQL1042C running a fenced routine (stored procedure/UDF)
Relation to this link http://www-01.ibm.com/support/docview.wss?uid=swg21399105 2015-01-11-13.38.19. ...
- (转)zabbix之生产案例
原文: https://www.abcdocker.com/abcdocker/category/zabbix/ 原文: https://chegva.com/1170.html
- SVN linux 服务器端配置
一. SVN 简单介绍 Subversion(SVN) 是一个开源的版本号控制系統, 也就是说 Subversion 管理着随时间改变的数据. 这些数据放置在一个中央资料档案库 (repository ...