angularJS中一些重复用的模板,可以封装起来,用这种自定义指令的方法实现。demo如下:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0" />
<script type="text/javascript" src="js/angular.min-1.6.x.js"></script>
</head>
<body ng-app='myapp1' >
<cus></cus>
</body>
<script type="text/javascript">
var app = angular.module('myapp1', []);
app.directive("cus",function(){
return {
restrict:'E',//'E':element元素 调用时:<cus></cus>
//'A':attribute属性 调用时<div cus></div>
//'C':class类 调用时<div class='cus'></div>
replace:true,//默认false,替换掉自定义的directive的名称
template:'<div>Hello world</div>',
link:function(){
alert('显示在这里呢!');
console.log('显示在这里呢!');
}
}
});
</script>
</html>