forked from bennadel/JavaScript-Demos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.htm
More file actions
180 lines (126 loc) · 3.85 KB
/
index.htm
File metadata and controls
180 lines (126 loc) · 3.85 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<!doctype html>
<html ng-app="Demo">
<head>
<meta charset="utf-8" />
<title>
Implementing Controller-As Using A Directive In AngularJS 1.0.8
</title>
<style type="text/css">
a[ ng-click ] {
color: red ;
cursor: pointer ;
text-decoration: underline ;
}
</style>
</head>
<body>
<h1>
Implementing Controller-As Using A Directive In AngularJS 1.0.8
</h1>
<!-- NOTE: As vm. -->
<div ng-controller="AppController" as="vm">
<p>
<a ng-click="vm.toggleSections()">Toggle Sections</a>
</p>
<div ng-switch="vm.section">
<!-- NOTE: As vm. -->
<p
ng-switch-when="firstSection"
ng-controller="FirstController" as="vm">
{{ vm.title }}
</p>
<!-- NOTE: As vm. -->
<p
ng-switch-when="secondSection"
ng-controller="SecondController" as="vm">
{{ vm.title }}
</p>
</div>
</div>
<!-- Load scripts. -->
<script type="text/javascript" src="../../vendor/jquery/jquery-2.0.3.min.js"></script>
<script type="text/javascript" src="../../vendor/angularjs/angular-1.0.8.min.js"></script>
<script type="text/javascript">
// Create an application module for our demo.
var app = angular.module( "Demo", [] );
// -------------------------------------------------- //
// -------------------------------------------------- //
// I control the root of the application.
app.controller(
"AppController",
function() {
// I represent the currently-selected section.
this.section = "firstSection";
// I switch the currently-selected section.
this.toggleSections = function() {
this.section = ( this.section === "firstSection" )
? "secondSection"
: "firstSection"
;
};
}
);
// -------------------------------------------------- //
// -------------------------------------------------- //
// I simply demonstrate that the "vm" reference works.
app.controller(
"FirstController",
function() {
this.title = "First controller, for the win!";
}
);
// I simply demonstrate that the "vm" reference works.
app.controller(
"SecondController",
function() {
this.title = "Second controller, heck yeah!";
}
);
// -------------------------------------------------- //
// -------------------------------------------------- //
// I attempt to implement (some of) the "controller-as" syntax in AngularJS
// 1.0.8. This treats the related ngController as an item of the "view model"
// in the HTML.
// --
// CAUTION: This does NOT make the scope unavailable in the view-model; it simply
// augments the scope with a circular reference.
app.directive(
"as",
function() {
// I copy the ngController instance reference into the scope so that it
// can be used to reference the Controller inside the View.
function prelink( scope, element, attributes, controller ) {
scope[ attributes.as ] = controller;
// Injecting the controller into the scope is likely to create a
// circular object reference: Controller -> Scope -> Controller. As
// such, let's take extra care to try to break up the reference in
// order to help with garbage collection.
scope.$on(
"$destroy",
function handleDestroyEvent() {
scope[ attributes.as ] = null;
// Also, now that we've created a closure, it may be helpful to
// clear "closed over" variable references.
scope = element = attributes = controller = null;
}
);
}
// Return the directive configuration.
// --
// NOTE: We are using the "prelink" phase so that the controller reference
// is available to the linking the phase of the nested directives.
// --
// NOTE: ngController runs at priority 500.
return({
link: {
pre: prelink
},
priority: 499,
require: "ngController",
restrict: "A"
});
}
);
</script>
</body>
</html>