-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path25-自定义指令-嵌套.html
50 lines (44 loc) · 1.41 KB
/
25-自定义指令-嵌套.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>自定义指令</title>
</head>
<body>
<div ng-controller="test" >
<hello><my-hello></my-hello></hello>
</div>
<script type="text/javascript" src="js/angular.min.js" ></script>
<script>
var m1 = angular.module('myApp',[]);
//自定义指令
m1.directive('hello',function(){
return {
restrict:'EACM',
replace:true,//将模板内容替换掉 自定义的标签
transclude:true,//可以解析hello自定指令中的嵌套指令
controller:function($scope){
this.name = 'test';
this.show = function(){alert(666);};
},
template:'<h2>hello angular<div ng-transclude></div></h2>'//调用自定义指令后的显示内容
}
});
//自定义指令的名称为驼峰式写法时, html中调用需要使用-连接两个单词
m1.directive('myHello',function(){
return {
restrict:'EACM',
replace:true,
require:'?^hello',//^对父级自定义指令中的变量进行查找,没有^是对同级的自定义指令中的变量进行查找
//想要容错 ? ,当找不到对应的变量时,不会报错处理
template:'<h3>hello -lili</h3>',
link:function (scope,element,attr,reController){
console.log(reController);
}
}
});
m1.controller('test',['$scope',function($scope){
}]);
</script>
</body>
</html>