本文实例讲述了angularjs中指令的四种基本形式。分享给大家供大家参考,具体如下:
指令的四种基本形式中,
注意注释型指令 m 的使用方法是 56126ba6dc694af05a8fdf8eebf41b7d 注意左右俩测必须有空格才会正常识别
所有指令是可以相互组合 的,不写restrict ,将会默认为a属性 指令要支持ie8 浏览器 一般最好将指令设置为属性
<!doctype html>
<html ng-app="myapp">
<head>
<meta charset="utf-8"/>
</head>
<body>
<elementtag>e</elementtag>
<div attr>a</div>
<div class="classnamw">c</div>
<!-- 注意注释变量两侧必须加上空格 否则不会正确执行这个指令 -->
<!-- directive:commit -->
<div></div>
<script src="./js/angular.min.js"></script>
<script>
var app = angular.module('myapp',[]);
app.directive('elementtag',function(){
return {
restrict:"e", //元素指令
link:function(scope,element,attrs){
console.log("this is a element");
}
};
})
.directive('attr',function(){
return {
restrict:"a", //属性指令
link:function(scope,element,attrs){
console.log("this is a attribute");
}
};
})
.directive('classnamw',function(){
return {
restrict:"c", //class 指令
link:function(scope,element,attrs){
console.log("this is a class");
}
};
})
.directive('commit',function(){
return {
restrict:"m", //注释指令
link:function(scope,element,attrs){
console.log("this is a commit");
}
};
});
</script>
</html>
希望本文所述对大家angularjs程序设计有所帮助。