-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathangularjs_require.html
More file actions
100 lines (95 loc) · 3.06 KB
/
angularjs_require.html
File metadata and controls
100 lines (95 loc) · 3.06 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>AngularJS·Hello AngularJS</title>
<link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.2/css/bootstrap.css">
<style>
.demo{
margin-bottom:10px;
border-top:1px solid #1b926c;
}
footer{
color:#fff;
background-color: #222;
border-color: #080808;
padding-top:60px;
margin-top:100px;
}
</style>
</head>
<body>
<div class="row">
<nav class="navbar navbar-default navbar-static-top">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="../index.html">首页</a>
</div>
<ul class="nav navbar-nav">
<li class="active"><a href="javascript:void(0)">Link</a></li>
<li><a href="javascript:void(0)">Link2</a></li>
</ul>
</div>
</nav>
</div>
<div class="container" ng-app="MyAPP">
<siteheader></siteheader>
<h3>自定义指令require属性示例</h3>
<common>
<mybutton1></mybutton1>
</common>
<sitefooter></sitefooter>
</div>
</body>
</html>
<script src="http://cdn.bootcss.com/angular.js/1.3.8/angular.js" type="text/javascript"></script>
<script>
var app=angular.module("MyAPP",[]);
/**
* require为字符串代表另外一个指令的名字。
* require会将控制器注入到其所指定的指令中,
* 并作为当前指令的链接函数的第四个参数。
* 字符串或数组元素的值是会在当前指令的作用域中使用的指令名称。
* 在任何情况下,ng编译器在查找子控制器时都会参考当前指令的模板。
*/
app.directive("common",function(){
return {
restrict:'ECMA',
controller:function($scope){
this.method1=function(){
alert("aaa");
};
this.method2=function(){
alert("bbbb");
}
}
}
});
app.directive('mybutton1',function(){
return {
restrict:'ECMA',
template:'<button id="btn1">按钮1</button><br/>' +
'<button id="btn2">按钮2</button>',
require:'?^common', //将其他指令作为link的第四个参数注入进来
link:function(scope,element,attr,anotherCtrl){
angular.element(document.querySelector('#btn1')).on('click',anotherCtrl.method1);
angular.element(document.querySelector('#btn2')).on('click',anotherCtrl.method2);
}
}
});
//公共区域,页头和页脚
app.directive('siteheader',function(){
return {
restrict:"E",
replace:true,
templateUrl:"views/header.html"
}
});
app.directive('sitefooter',function(){
return {
restrict:"E",
replace:true,
templateUrl:"views/footer.html"
}
});
</script>