-
Notifications
You must be signed in to change notification settings - Fork 0
/
exampleCode.html
195 lines (146 loc) · 4.75 KB
/
exampleCode.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>AngularJs - Implementation Example</title>
<link rel="icon" href="img/favicon.png" type="image/x-icon" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="css/exampleStyle.css">
<!-- For syntax highlighting -->
<link rel="stylesheet" href="bower_components/highlightjs/styles/solarized_dark.css" id="highlight-theme">
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<pre class="hiCode jsCode">
<code class="javascript" data-trim>
//Implementazione spicciola delle parti di AngularJS: Scope e Controller
var $parse = function (expression) {
var result = function (scope) {
return scope[expression];
};
result.assign = function (scope, newValue) {
scope[expression] = newValue;
};
return result;
};
function Scope() {
this.$$watchers = [];
}
Scope.prototype.$watch = function (watcher, listenerFn) {
var watcherFn;
if (typeof watcher == 'string') {
watcherFn = $parse(watcher);
} else {
watcherFn = watcher;
}
this.$$watchers.push({
watcherFn: watcherFn,
listenerFn: listenerFn
});
};
Scope.prototype.$digest = function () {
this.$$watchers.forEach(function (watch) {
var newValue = watch.watcherFn(this);
var oldValue = watch.last;
if (newValue != oldValue) {
watch.listenerFn(newValue, oldValue, this);
watch.last = newValue;
}
}.bind(this));
};
Scope.prototype.$apply = function (exprFn) {
try {
exprFn();
} finally {
this.$digest();
}
};
var $$directives = {};
var $directive = function (name, directiveFn) {
if (directiveFn) {
$$directives[name] = directiveFn;
}
return $$directives[name];
};
var $compile = function (element, scope) {
element.children.forEach = Array.prototype.forEach;
element.attributes.forEach = Array.prototype.forEach;
element.children.forEach(function (child) {
$compile(child, scope);
});
element.attributes.forEach(function (attribute) {
var directiveFn = $directive(attribute.name);
if (directiveFn) {
directiveFn(scope, element, element.attributes);
}
});
};
$directive('ng-bind', function (scope, element, attributes) {
scope.$watch(attributes['ng-bind'].value, function (newValue) {
element.innerHTML = newValue;
});
});
$directive('ng-model', function (scope, element, attributes) {
// TWO-WAY data binding
scope.$watch(attributes['ng-model'].value, function (newValue) {
element.value = newValue;
});
// aggiornamento del componente (secondo data-binding)
element.addEventListener('keyup', function () {
scope.$apply(function () {
$parse(attributes['ng-model'].value).assign(scope, element.value);
});
});
});
// fine definizione
//Inizializzazione di Angular (BOOTSTRAP)
//console.log($$directives);
var rootScope = new Scope();
$compile(document.body, rootScope);
//parte specifica della pagine,
//messa qui per evitare l'inizializzazione del modulo e il controller
rootScope.$watch('title', function (newValue, oldValue) {
console.log('listened title', oldValue, '->', newValue);
});
rootScope.$apply(function () {
rootScope.title = 'AngularJS is Hard';
rootScope.name = 'World';
});
setTimeout(function () {
rootScope.$apply(function () {
rootScope.title = 'AngularJS is easy !!';
});
}, 5000);
</code>
</pre>
<pre class="hiCode hmtlCode">
<code class="html" data-trim>
<h1 ng-bind="title">Not Working</h1>
<h2>
<input type="text" ng-model="name" />
</h2>
<h2>Hello <span ng-bind="name"></span></h2>
</code>
</pre>
<div class="credits">VIEW <a href="example.html">RESULT</a> - Thanks to <a href="https://github.com/Swiip/angular-from-scratch">Swiip</a>
- See the <a href="www.youtube.com/watch?v=Mk2WwSxK218">video</a>
</div>
<script src="bower_components/jquery/dist/jquery.min.js"></script>
<script src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="bower_components/highlightjs/highlight.pack.js"></script>
<script>
$(document).ready(function() {
hljs.initHighlightingOnLoad();
$('div.credits').click(function(){
$('div.credits').hide();
});
});
</script>
<script src="js/AngularJsImplementation.js"></script>
</body>
</html>