-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-directive-link.html
87 lines (86 loc) · 3.29 KB
/
angular-directive-link.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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>angular-directive-link</title>
<script type="text/javascript" src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body ng-app="app">
<stu1></stu1>
<hr>
<app-demo></app-demo>
<script type="text/javascript">
var app = angular.module('app', []);
app.directive('stu1', function(){
return {
restrict: 'E',
template: '<p>1.<stu2></stu2></p>',
controller: function(){
// controller从最外部的stu1的link函数开始,依次执行
console.log('stu1 controller running');
},
link: function(scope){
console.log('stu1 running');
}
};
});
app.directive('stu2', function(){
return {
restrict: 'E',
template: '<p>2.<stu3></stu3></p>',
controller: function(){
console.log('stu2 controller running');
},
link: function(scope){
console.log('stu2 running');
}
};
});
app.directive('stu4', function(){
return {
restrict: 'E',
template: '<p>4.</p>',
controller: function($scope){
$scope.name = 'Amber';
console.log('stu4 controller running');
},
link: function(scope, el){
// 先controller,后link
el.append("<p>Hey!</p>")
// link从最根部的stu4的link函数开始,依次执行
console.log("stu4 link running");
}
};
});
app.directive('stu3', function(){
return {
restrict: 'E',
template: '<p>3.<stu4></stu4></p>',
controller: function(){
console.log('stu3 controller running');
},
link: function(scope){
console.log('stu3 link running');
}
};
});
app.directive('appDemo', function(){
return {
restrict: 'E',
template: '<div>Click Me</div>',
link: function(scope, element, attr, ctrl){
element.bind('click', function(){
console.log('绑定点击事件');
});
element.append('<p>link增加段落</p>');
element.css({
'display': 'inline-block',
'background-color': '#999',
'color': 'yellow'
});
}
}
});
</script>
</body>
</html>