AngularJS - Directive Restrictions
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了AngularJS - Directive Restrictions,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2997字,纯文字阅读大概需要5分钟。
内容图文
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.
From here, instead of having superman as an element, let’s do a div with superman as an attribute:
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.
1 <div ng-app="superhero"> 2 <div 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.
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”)
Now we should have a div with both “superman” and “flash” as attributes
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”
原文:http://www.cnblogs.com/lucker/p/3783552.html
内容总结
以上是互联网集市为您收集整理的AngularJS - Directive Restrictions全部内容,希望文章能够帮你解决AngularJS - Directive Restrictions所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。