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  


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:

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 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”

Share your progress!

Reference
Url: http://www.thinkster.io/angularjs/rep5re7gTM/angularjs-directive-restrictions

AngularJS - Directive Restrictions,布布扣,bubuko.com

时间: 2024-10-10 23:38:06

AngularJS - Directive Restrictions的相关文章

学习AngularJs:Directive指令用法(完整版)

这篇文章主要学习AngularJs:Directive指令用法,内容很全面,感兴趣的小伙伴们可以参考一下 本教程使用AngularJs版本:1.5.3 AngularJs GitHub: https://github.com/angular/angular.js/ AngularJs下载地址:https://angularjs.org/ 摘要:Directive(指令)笔者认为是AngularJ非常强大而有有用的功能之一.它就相当于为我们写了公共的自定义DOM元素或LASS属性或ATTR属性,并

angularjs directive 实例 详解

前面提到了angularjs的factory,service,provider,这个可以理解成php的model,这种model是不带html的,今天所说的directive,也可以理解成php的model,也可以理解成插件,只不过这种model是带html的,例如:php的分页函数. 一,angularjs directive的常用格式,以及参数说明 1,直接return phonecatDirectives = angular.module('phonecatDirectives', [])

Angularjs directive全面解读(1.4.5)

说到Angularjs directive即指令,可以这么说Angularjs的灵魂就是指令,学会Angularjs指令那么你的Angularjs的武功就修炼了一半了,当然这只是鄙人的一点点独到见解,废话不多说,以下是Angularjs 指令的运用及源码实现解读. 学习Angularjs directive那么我们要带3个问题来学习: 1. 什么是angular 指令?2. 指令如何定义存储的?3. 指令如何编译运行的? 1.首先第一点解读什么是指令: At a high level, dire

angularJS Directive学习

Directive 指令 直接上实例 index.html <!doctype html> <html ng-app="drag"> <head> <script src="http://code.angularjs.org/angular-1.1.0.min.js"></script> <script src="script.js"></script> <

跟我学AngularJs:Directive指令用法解读(下)

此文接 跟我学AngularJs:Directive指令用法解读(上) 8.transclude 如果不想让指令内部的内容被模板替换,可以设置这个值为true.一般情况下需要和ngTransclude指令一起使用. 比如:template:"<div>hello every <div ng-transclude></div></div>",这时,指令内部的内容会嵌入到ng-transclude这个div中.也就是变成了<div>

AngularJS:directive自定义的指令

除了 AngularJS 内置的指令外,我们还可以创建自定义指令. 你可以使用 .directive 函数来添加自定义的指令. 要调用自定义指令,HTML 元素上需要添加自定义指令名. 使用驼峰法来命名一个指令, runoobDirective, 但在使用它时需要以 - 分割, runoob-directive: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script

[整理]通过AngularJS directive对bootstrap日期控件的的简单包装

最近项目上了AngularJS,而原来使用的日期控件的使用方式也需要改变,于是开始了倒腾,看来写官方的例子,可以使用AngularJS的directive做简单的处理,这样在html里直接使用申明的的形式即可使用了. <!doctype html> <html ng-app="datepickerApp"> <head> <link type="text/css" rel="stylesheet" hre

angularJS directive中的controller和link function辨析

在angularJS中,你有一系列的view,负责将数据渲染给用户:你有一些controller,负责管理$scope(view model)并且暴露相关behavior(通过$scope定义)给到view:你有一些directive,负责将user interaction和$scope behavious link起来.但是还有一样东西: a directive controller.这个directive controller子一个directive的context中定义,但是它又可以被in

angularJS directive详解

前言 最近学习了下angularjs指令的相关知识,也参考了前人的一些文章,在此总结下. 欢迎批评指出错误的地方. Angularjs指令定义的API AngularJs的指令定义大致如下 angular.module("app",[]).directive("directiveName",function(){     return{      //通过设置项来定义     }; }) 其中return返回的对象包含很多参数,下面一一说明 1.restrict (