Repository navigation
Expand file tree
/
Copy pathnotes.txt
More file actions
283 lines (202 loc) · 6.28 KB
/
Copy pathnotes.txt
File metadata and controls
283 lines (202 loc) · 6.28 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
Minimum Setup:
Add angular.min.js to js folder
reference angular.min.js in html (above </body> )
<html data-ng-app="app">
Sanity check:
{{2+2}}
---------------
Add app.js
(function () {
'use strict';
var app = angular.module('app', [/*dependency injection goes here*/]);
})();
--------------
Add first controller: basicController.js
(function () {
'use strict';
var controllerId = 'basicCtrl';
angular.module('app').controller(controllerId, ['$scope', basicCtrl]);
function basicCtrl($scope) {
var vm = this;
};
})();
------------------
add some html
data-ng-controller="basicCtrl as vm" to <div class="container">
add refernce to app.js
add reference to basicController.js
------------------
add test object
vm.testObject = {
title: "Contact Information",
someDataInArray: ["Text 1", "Text 2", "Text 3"],
innerObject: {
innerObjectId: 1,
innerObjectText: "This is text in inner object"
},
innerObjectArray: [
{
innerObjectId: 1,
innerObjectText: "This is text in inner object"
},
{
innerObjectId: 2,
innerObjectText: "This is yet another text in inner object"
}
],
user: {
name: '',
email: '',
age: ''
}
};
-----------------
add html
<p class="lead myDivider">
<span data-ng-repeat="text in vm.testObject.someDataInArray">
{{text}}
</span>
</p>
<p class="lead myDivider">
{{vm.testObject.innerObject.innerObjectId}} = {{vm.testObject.innerObject.innerObjectText}}
</p>
----------------
add js code to controller:
vm.buttonClicked = function () {
alert("button was clicked");
};
vm.anotherButtonClicked = function (fromView) {
alert("Hello " + fromView);
};
add html:
<p class="lead myDivider" data-ng-click="vm.buttonClicked()">
[CLICK]
</p>
<p class="lead myDivider" data-ng-click="vm.anotherButtonClicked(vm.testObject.title)">
[CLICK ANOTHER]
</p>
------------------
connection between view and controller - clicking
js:
vm.numberClicked = 0;
vm.anotherButtonClicked = function (fromView) {
alert("Hello " + fromView + ' [' + vm.numberClicked + ']');
vm.numberClicked++;
};
html:
<p class="lead myDivider" data-ng-click="vm.anotherButtonClicked(vm.testObject.title)">
[CLICK ANOTHER ({{vm.numberClicked}})]
</p>
------------------------------------
odd / even classes, repeater
<p class="lead"
data-ng-repeat="innerObj in vm.testObject.innerObjectArray"
data-ng-class-even="'myEvenClass'" data-ng-class-odd="'myOddClass'">
[{{$index}}]
{{innerObj.innerObjectId}} = {{innerObj.innerObjectText}}
</p>
----------------------------
Add new line (object)
js:
vm.addNewObject = function() {
vm.testObject.innerObjectArray.push(
{
innerObjectId: vm.testObject.innerObjectArray.length + 1,
innerObjectText: 'What do you get if you multiply six by nine?'
});
vm.numberClicked++;
if (vm.numberClicked % 3 == 0) {
vm.testObject.innerObjectArray.push(
{
innerObjectId: vm.testObject.innerObjectArray.length + 1,
innerObjectText: 'Answer is: 42.'
});
}
};
html:
add: data-ng-click="vm.addNewObject()" to <p> with repeater
-------------------------
show directives:
Add new html: partial.html with content:
<p class="lead"
data-ng-repeat="innerObj in vm.testObject.innerObjectArray"
data-ng-class-even="'myEvenClass'" data-ng-class-odd="'myOddClass'"
data-ng-click="vm.addNewObject()">
[{{$index}}]
{{innerObj.innerObjectId}} = {{innerObj.innerObjectText}}
</p>
add directive module:
angular.module('app').directive('partialExample', function () {
return {
restrict: 'E',
templateUrl: 'partial.html'
};
});
add partial to html:
<partial-example />
explain camel casing replacement (aA --> a-a)
explain restrictions: E - element, A - attribute, C - class, M - comment
Attribute: <div partial-example></div>
--------------
Two way binding - remove anything we did so far, especially partial :-)
<h2>Two way binding</h2>
<input type="text" data-ng-model="vm.testObject.title" />
Talk about major ideas of Angular - working with data (model) on client, sending it back to server via ajax post, no form method="post"
asp.net mvc helpers
--------------
Quick demo of form validation:
html:
<form novalidate class="myAngularForm" name="form">
<div class="form-group">
<label for="inputName" class="col-sm-2 control-label">Name:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputName"
data-ng-model="vm.user.name" required>
</div>
</div>
<div class="form-group">
<label for="inputEmail" class="col-sm-2 control-label">E-mail:</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="inputEmail"
data-ng-model="vm.user.email" required>
</div>
</div>
</form>
novalidate - turn off native browser validation
required - Angular directive (attribute)
show css classes (valid, invalid)
input type="email" = Angular checking for proper email format
Add some custom validation:
this goes before </form>:
<div class="form-group">
<label for="inputAge" class="col-sm-2 control-label">Age:</label>
<div class="col-sm-10">
<input type="number" class="form-control" id="inputAge" name="inputAge"
data-ng-model="vm.user.age" min="18" max="40" integer />
</div>
<span class="help-block" data-ng-show="form.inputAge.$error.integer">This is not valid integer!</span>
<span class="help-block" data-ng-show="form.inputAge.$error.min || form.size.$error.max">
Sorry, must be older than 18 and younger than 40. Call it age discrimination :-)
</span>
</div>
+ js:
var INTEGER_REGEXP = /^\-?\d+$/;
angular.module('app').directive('integer', function () {
return {
require: 'ngModel',
link: function (scope, elm, attrs, ctrl) {
ctrl.$parsers.unshift(function (viewValue) {
if (INTEGER_REGEXP.test(viewValue)) {
// it is valid
ctrl.$setValidity('integer', true);
return viewValue;
} else {
// it is invalid, return undefined (no model update)
ctrl.$setValidity('integer', false);
return undefined;
}
});
}
};
});
---------------------------------END BASICS