-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.html
More file actions
1447 lines (1218 loc) · 42.7 KB
/
Copy pathindex.html
File metadata and controls
1447 lines (1218 loc) · 42.7 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>AngularJS Foundations</title>
<meta name="author" content="ti&m AG, Stefan Zollinger">
<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, minimal-ui">
<link href='//fonts.googleapis.com/css?family=Roboto:300,700,400,100' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/ti8m.css" id="theme">
<!-- Code syntax highlighting -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<div class="slides">
<section class="splash">
<h1>AngularJS Foundations</h1>
<h2>Workshop</h2>
<p>
<img src="img/ng-logo.svg" width="400" />
</p>
<p>
<small>18. Januar 2016</small>
</p>
<div class="footer">
<p>
Mato Ilic • Stefan Zollinger
</p>
<ul>
<li>Consulting.</li>
<li>Design.</li>
<li>Agile Projects.</li>
<li>Products.</li>
<li>Innovation Hosting.</li>
</ul>
</div>
</section>
<section>
<h2>Warum dieser Kurs?</h2>
</section>
<section>
<h2>
Einstieg in AngularJS ist zwar einfach, aber…
</h2>
<ul>
<li class="fragment">keine (offiziellen) Konventionen.</li>
<li class="fragment">viele verschiedene Ansätze im Netz.</li>
<li class="fragment">AngularJS beinhaltet viel "Magic" der man nicht blind vertrauen sollte.</li>
</ul>
</section>
<section>
<h2>Ziele</h2>
<ul>
<li class="fragment">Guidelines und Struktur geben.</li>
<li class="fragment">AngularJS Konzepte und deren Anwendungen klären.</li>
<li class="fragment">"Magische" Teile von AngularJS durchleuchten.</li>
</ul>
</section>
<section>
<h2>Setup Workshop Projekt</h2>
<p>
Schritte um das Projekt zu installieren:
</p>
<pre>
<code class="bash" data-trim>
$ git clone https://github.com/matoilic/AngularWorkshop
$ cd AngularWorkshop
$ npm install -g gulp
$ npm install
$ gulp
</code>
</pre>
</section>
<section>
<h2>Übersicht Workshop Projekt</h2>
<ul>
<li>Abhängigkeiten</li>
<li>Verzeichnisstrutkur</li>
<li>Build System</li>
</ul>
</section>
<section>
<h2>Abhängigkeiten (npm)</h2>
<p>
Definiert in package.json:
</p>
<pre>
<code class="json" data-trim>
{
"name": "my-package",
"description": "An example package",
"version": "0.0.1",
"devDependencies": {
"gulp": "^3.8.11",
"gulp-connect": "^2.2.0",
"gulp-protractor": "^1.0.0"
},
"dependencies": {
"angular": "^1.4.0",
"angular-resource": "^1.4.0",
"angular-ui-router": "^0.2.15",
"normalize.css": "^3.0.3"
}
}
</code>
</pre>
<aside class="notes">
dependencies ➾ Laufzeitabhängigkeiten (z.B. Angular oder Bootstrap)<br>
devDependencies ➾ Abhängigkeiten für Entwicklung (z.B. Gulp oder Karma)<br>
Verwaltung mittels NPM CLI
</aside>
</section>
<section>
<h2>Top Level Struktur</h2>
<pre>
<code class="bash" data-trim>
.git/
node_modules/
src/
.editorconfig
.gitignore
.jshintrc
gulpfile.js
index.html
package.json
protractor.conf.js
webpack.conf.js
</code>
</pre>
<aside class="notes">
- Struktur ist ähnlich bei den meisten modernen Frontend Projekten<br><br>
Kurz erklären was warum.
</aside>
</section>
<section>
<h2>AngularJS Struktur</h2>
<pre>
<code class="bash" data-trim>
components/
/address-book/
/application/
/welcome-state
/index.js
/welcome-state.js
/welcome-controller.js
/welcome.html
images/
stylesheets/
</code>
</pre>
<aside class="notes">
Kurz erklären was warum.
</aside>
</section>
<section>
<h2>Standard Projektstruktur</h2>
<pre>
<code class="bash" data-trim>
/controllers
/home-controller.js
/other-controller.js
/even-more-controller.js
/services
/home-service.js
/other-service.js
/3rd-service.js
/views
/home.html
/other.html
/more-templates.html
</code>
</pre>
<p>
<img width="200" height="200" src="http://cdn.meme.am/instances/500x/11541951.jpg">
</p>
</section>
<section>
<h2>Vorteile fachlicher Aufteilung</h2>
<ul class="small">
<li>Alle Teile einer Komponente sind unter einem Dach.</li>
<li>Komponenten sind einfacher wiederverwendbar.</li>
<li>Die Auswirkungen von Code-Änderungen sind besser nachvollziehbar.</li>
</ul>
</section>
<section>
<h2>Backend</h2>
<p>
API-Endpoint<br>
<a href="https://nameless-sierra-8010.herokuapp.com/api">https://nameless-sierra-8010.herokuapp.com/api</a>
</p>
<p>
API-Browser<br>
<a href="https://nameless-sierra-8010.herokuapp.com/explorer">https://nameless-sierra-8010.herokuapp.com/explorer</a>
</p>
</section>
<section>
<h2>Workflow Automation mit Gulp</h2>
<p>gulpfile.js</p>
<h3>Default-Task</h3>
<ul>
<li><strong>connect</strong> Startet den Entwicklungsserver.</li>
</ul>
<h3>"test"-Task</h3>
<ul>
<li><strong>test</strong> Startet Protractor end-to-end Tests.</li>
</ul>
</section>
<section>
<h2>Bundling mit webpack</h2>
<ul>
<li>Server mit Livereload während der Entwicklung.</li>
<li>Resource-Bundling für Production.</li>
</ul>
</section>
<section>
<h2>import ⟺ export</h2>
<dl>
<dt>import</dt>
<dd>Importiert die öffentlichen Teile eines Paketes in eine lokale Variable / Referenz.</dd>
<dt>export</dt>
<dd>Macht Komponententeile zugänglich für andere Module.</dd>
</dl>
</section>
<section>
<h2>Modul-Export</h2>
<pre><code class="javascript" data-trim>
class ExampleController {
// ...
}
export default [
'dependency1',
'dependency2',
ExampleController
];
</code></pre>
</section>
<section>
<h2>Modul-Import</h2>
<pre><code class="javascript" data-trim>
import angular from 'angular';
import ExampleController from './example-controller';
const dependencies = [
];
export default angular
.module('example-module', dependencies)
.controller('ExampleController', ExampleController);
</code></pre>
</section>
<section>
<h2>AngularJS im Überblick</h2>
<img src="/img/ng-arch.svg">
</section>
<section>
<h2>Angular Modul</h2>
<p>Sammlung von Klassen, Funktionen und Objekten.</p>
</section>
<section>
<h2>Neues Modul definieren</h2>
<pre>
<code class="javascript" data-trim>
angular.module('myApp', []);
</code>
</pre>
<h2>Modul 'myApp' referenzieren</h2>
<pre>
<code class="javascript" data-trim>
angular.module('myApp');
</code>
</pre>
</section>
<section>
<h2>Modul-Laufzeit</h2>
<pre>
<code class="javascript" data-trim>
angular
.module('myModule', ['ui-router'])
.config(function($stateProvider){})
.run(initializationFn)
.provider('myProvider', function(){})
.controller('myController', '')
</code>
</pre>
<h3>Ablauf</h3>
<ul>
<li>Modul wird mit Dependency 'ui-router' initialisiert
<li>config() wird ausgeführt
<li>run() wird ausgeführt
</ul>
</section>
<section>
<h2>Dependency Injection</h2>
<ul>
<li class="fragment">Injector bietet einen Weg Module nach Namen zu referenzieren.</li>
<li class="fragment">Jede AngularJS App hat einen Injector.</li>
<li class="fragment">Jedes Modul wird nur einmal instanziert.</li>
<li class="fragment">Injector löst Dependencies in Funktionen auf.</li>
</ul>
<pre class="fragment">
<code class="javascript" data-trim>
class MyService {
constructor(dependencyA, dependencyB) {
// Do something with dependencyA
dependencyA.doSomething();
}
}
</code>
</pre>
</section>
<section>
<h2>Dependency Injection</h2>
<ul>
<li><strong>Achtung:</strong> Automatische DI funktioniert nur solange der Code nicht minifiziert ist.
<li><strong>Besser:</strong> Dependencies explizit angeben.
</ul>
<pre>
<code class="javascript" data-trim>
class MyService {
constructor(DependencyA) { }
}
angular
.module('myApp')
.service('MyService', ['DependencyA', MyService]);
</code>
</pre>
</section>
<section>
<h2>Tipp</h2>
<p>Mit 'ngStrictDi' explizite DI Annotationen forcieren.</p>
<pre>
<code class="html" data-trim><body ng-app="myApp" ng-strict-di="true"></code>
</pre>
</section>
<section>
<h2>Views (Templates)</h2>
<p>
HTML Dateien.
</p>
<pre>
<code type="html" data-trim>
<div>
<h1>Address book</h1>
<p>
Hello World.
</p>
</div>
</code>
</pre>
<p>
Templates müssen immer ein Root-Element besitzen.
</p>
</section>
<section>
<h2>Routing</h2>
<p>Verbindet States und ihre Views mit spezifischen URLs.</p>
</section>
<section>
<h2>Beispiel einer Route</h2>
<pre>
<code type="javascript" data-trim>
function contactListState($stateProvider) {
$stateProvider.state('contactList', {
url: '/contact-list',
templateUrl: 'contact-list.html',
resolve: { }
});
}
angular
.module('ContactList')
.config(['$stateProvider', contactListState]);
</code>
</pre>
</section>
<section>
<h2>ng-route vs ui-router</h2>
<ul >
<li class="fragment">
<h3>ng-route</h3>
<ul>
<li>Standard-Komponente von Angular.</li>
<li>Erlaubt nur einfache Routes und Views.</li>
</ul>
</li>
<li class="fragment">
<h3>ui-router</h3>
<ul>
<li>Kann verschachtelte Views darstellen und komplexe States abbilden.</li>
<li>De-facto Standard.</li>
</ul>
</li>
</ul>
<h3 class="fragment">Empfehlung: immer ui-router verwenden.</h3>
</section>
<section class="hands-on" data-state="hands-on" >
<h2>Hands-on</h2>
<h3>Neue Route für Kontaktliste erstellen. </h3>
<ul>
<li>Neues Modul ContactList (analog 'Welcome') erstellen.</li>
<li>View Template: contact-list.html.</li>
<li>Route: /contact-list.</li>
</ul>
<p>
<img width="200" height="200" src="http://cdn.meme.am/instances/500x/57106019.jpg">
</p>
</section>
<section>
<h2>Directives</h2>
<ul>
<li class="fragment">Wiederverwendbare UI-Komponenten.</li>
<li class="fragment">Erweiterte HTML Tags bzw. Web Components.</li>
<li class="fragment">Darf DOM Manipulationen machen.</li>
<li class="fragment">Kann HTML Elemente mit neuem Verhalten erweitern.</li>
</ul>
</section>
<section>
<h2>Beispiele für Direktiven</h2>
<div class="fragment">
<h3>ng-model</h3>
<pre>
<code type="html" data-trim>
<input type="text" name="searchTerm" ng-model="searchTerm"/>
<p>{{ searchTerm }}</p>
</code>
</pre>
</div>
<div class="fragment">
<h3>ng-class</h3>
<pre>
<code type="html" data-trim>
<p ng-class="{'expanded': textExpanded}">Some content</p>
</code>
</pre>
</div>
<div class="fragment">
<h3>ng-if</h3>
<pre>
<code type="html" data-trim>
<p ng-if="myController.isShown">Content goes here.</p>
</code>
</pre>
</div>
</section>
<section data-state="hands-on" data-markdown>
##Hands-on
* Füge der contact-list View ein Input Feld hinzu.
* Verwende <code>ng-model</code> um den Input einer Variable zuzuweisen.
* Gib die Variable auf der Seite aus.
### Bonus
* Falls der Input Wert 'spin' ist, soll dem Element eine CSS Klasse mit dem Namen 'spin' hinzugefügt werden (Tipp: ng-class).
</section>
<section>
<h2>Controller</h2>
<p>Controller stellen der View Funktionen zur Verfügung und repräsentieren den State der View. Controller agieren als View Model.</p>
</section>
<section>
<h2>Was nicht in den Controller gehört</h2>
<ul>
<li class="fragment">Business-Logik.</li>
<li class="fragment">State mit anderen Controllern teilen.</li>
<li class="fragment">Output formatieren.</li>
</ul>
<aside class="notes">
<ul>
<li><strong>Business-Logik.</strong> Gehört in Services</li>
<li><strong>Output formatieren und filtern.</strong> Dazu sind AngularJS Filter da</li>
<li><strong>State mit anderen Controllern teilen.</strong> Dafür gibt es die AngularJS Services</li>
</ul>
</aside>
</section>
<section>
<h2>Controller definieren</h2>
<pre>
<code class="javascript" data-trim>
class ContactListController {
constructor($scope, $stateParams) {
}
}
angular
.module('ContactList', [])
.controller('ContactListController', [
'$scope',
'$stateParams',
ContactListController
]);
</code>
</pre>
</section>
<section>
<h2>Controller einer View zuweisen</h2>
<pre class="fragment">
<code class="javascript" data-trim>
function contactListState($stateProvider) {
$stateProvider.state('contact-list', {
url: '/contact-list',
templateUrl: 'contact-list.html',
controller: 'ContactListController as $ctrl'
});
}
angular
.module('ContactList')
.config(['$stateProvider', contactListState]);
</code>
</pre>
<aside class="notes">
Frage: Wo würde man die Zuweisung erwarten?
</aside>
</section>
<section data-state="hands-on">
<h2>Hands-on</h2>
<ul>
<li>Erstelle einen neuen Controller 'ContactListController' im Modul ContactList.</li>
<li>Definiere einen Array mit den Namen der Kontakte.</li>
<li>Liste diese Namen in der View auf (siehe: ng-repeat Direktive).</li>
</ul>
</section>
<section>
<h2>Filter</h2>
<ul>
<li class="fragment">Transformieren Werte und Objekte im View Template.</li>
<li class="fragment">Sind Chainable durch den Pipe Separator.</li>
</ul>
<div class="fragment">
<p>
Beispiel:
</p>
<pre>
<code type="html" data-trim>
{{ 1434098007198 | date:'shortDate' }}
</code>
</pre>
</div>
</section>
<section data-state="hands-on" data-markdown>
## Hands-on
Verwende den AngularJS Filter "filter" um die Kontaktliste nach dem Wert aus Suchfeld zu filtern.
### Bonus
Erstelle einen Filter, der den ersten Buchstaben eines Strings immer gross ausgibt.
Es gibt keine “capitalize” Funktion in Javascript, deshalb hier der Code:
``` javascript
var myString = 'angular';
myString.charAt(0).toUpperCase() + myString.substring(1);
// Prints 'Angular'
```
</section>
<section data-markdown>
## Formulare & Validierung
``` html
<form name="form" novalidate>
<p>
<label for="fieldEmail">Email</label>
<input type="email" ng-model="user.email"
name="fieldEmail" id="fieldEmail" required>
</p>
<p ng-show="form.$submitted || form.fieldEmail.$touched">
<span ng-show="form.fieldEmail.$error.required">Tell us your email.</span>
<span ng-show="form.fieldEmail.$error.email">This is not a valid email.</span>
</p>
<p>
<button type="submit" ng-disabled="!form.$valid">Submit</button>
</p>
</form>
```
</section>
<section data-state="hands-on">
<h2>Hands-on</h2>
<ul>
<li>Erstelle eine Detailseite für einen Kontakt.</li>
<li>
Formularfelder für
<ul>
<li>Name</li>
<li>Email</li>
<li>Telefonnummer</li>
</ul>
</li>
<li>Validiere den Namen (required) und die E-Mail (korrekte E-Mail Adresse).</li>
</ul>
</section>
<section>
<h2>Scope-Funktionen</h2>
<dl>
<dt>$on</dt>
<dd>Registriert einen Event-Listener.</dd>
<dt>$emit</dt>
<dd>Sendet einen Event die Hierarchie aufwärts bis zum $rootScope.</dd>
<dt>$broadcast</dt>
<dd>Sendet einen Event die Hierarchie abwärts vom der Quelle aus.</dd>
</dl>
</section>
<section>
<h2>Events</h2>
<img src="/img/events.svg">
</section>
<section>
<h2>System Events</h2>
<dl>
<dt class="fragment">$destroy</dt>
<dd class="fragment">Wenn ein scope zerstört wird.</dd>
</dl>
<p class="fragment small">
That's it, $destroy ist der einzige…
</p>
</section>
<section>
<h2>Scope Vererbung</h2>
<dl>
<dt>$rootScope</dt>
<dd>Parent aller Scopes.</dd>
<dd>Alle Scopes erben prototypisch von $rootScope bzw. Parent-Scope.</dd>
</dl>
</section>
<section>
<h2>Scope Isolation</h2>
<ul>
<li>Betrifft Direktiven.</li>
<li>Der isolierte Child Scope verändert den Parent Scope nicht (ng-if, ng-switch, ng-repeat, ng-view, ng-include).</li>
<li>Primitives die an eine Direktive mit isoliertem Scope übergeben werden, verlieren das 2-way Binding.</li>
</ul>
</section>
<section>
<h2>Data Binding</h2>
<dl>
<dt>View</dt>
<dd>{{ myController.startDate | date:'simple' }}</dd>
<dt>Controller</dt>
<dd>this.startDate = new Date();</dd>
<dd><br><br></dd>
<dd>Angular kümmert sich automatisch um die Synchronisation zwischen den Daten in HTML und JavaScript und zwar in beide Richtungen. Falls etwas in JS geändert wird, zieht es Angular für das HTML nach und umgekehrt.</dd>
</dl>
</section>
<section>
<h2>Data Binding: Digest Cycle</h2>
<ul>
<li>Für jedes Objekt welches in einer View verwendet wird, wird eine Funktion in die Watch-List hinzugefügt.</li>
<li>Sobald der Wert eines Objektes ändert, wird ein $digest Cycle ausgelöst.</li>
<li>Im $digest Cycle werden alle Objekte auf Änderungen geprüft. Falls es Änderungen gab, wird der Cycle erneut ausgeführt, bis es keine mehr gibt.</li>
<li>Anschliessend werden die Views neu gerendert.</li>
</ul>
</section>
<section>
<h2>Digest Cycle</h2>
<img src="img/digest-cycle.svg">
</section>
<section>
<h2>Controller, Scope und controllerAs</h2>
<p>Es gibt zwei Varianten wie Controller und Scopes eingesetzt werden können.</p>
<dl>
<dt class="fragment">Standard:</dt>
<dd class="fragment">Variablen und Methoden werden direkt auf dem Scope definiert. <img src="img/death.svg" style="height: 1em; vertical-align: text-bottom;"></dd>
<dt class="fragment">'controllerAs'</dt>
<dd class="fragment">Controller wird als eigenes Objekt an den Scope übergeben. Variablen und Methoden werden auf dem Controller definiert. Der Controller agiert als View Model.</dd>
</dl>
</section>
<section>
<h2>Beispiel</h2>
<p>Route Definition:</p>
<pre>
<code type="javascript" data-trim>
function contactListState($stateProvider) {
$stateProvider.state('contact-list', {
templateUrl: 'contact-list.html',
controller: 'ContactListController as contactList'
});
}
angular
.module('ContactList')
.config(['$stateProvider', contactListState]);
</code>
</pre>
<p>View:</p>
<pre>
<code type="html" data-trim>
<ul>
<li ng-repeat="contact in addressList.contacts">{{ contact.name }}</li>
</ul>
</code>
</pre>
</section>
<section>
<h2>Vorteile von 'controllerAs'</h2>
<ul>
<li class="fragment">Einfach nachvollziehbar von wo welche Daten kommen.</li>
<li class="fragment">Vereinfachtes Testing von View-Funktionalität.</li>
<li class="fragment">Verbesserte Wartbarkeit der Codebasis.</li>
</ul>
<h3 class="fragment">Empfehlung ➾ immer controllerAs verwenden.</h3>
</section>
<section data-markdown>
## URL Parameter (ui-router)
Definition von URL Parametern im State:
``` javascript
$stateProvider.state('contactDetail', {
url: '/detail/:name'
/* ... */
});
```
$stateParams Service ermöglicht einen einheitlichen Zugriff auf URL-Parameter:
``` javascript
class MyController{
constructor($stateParams) {
this.name = $stateParams.name;
}
}
```
</section>
<section data-state="hands-on" data-markdown>
## Hands-on
* Erstelle einen Controller für die Detail-View mit controllerAs Notation.
* Verlinke den Kontakt aus der Liste mit der Detailansicht. Verwende dafür die URL Parameter von ui-router.
* Die Route sollte /detail/:name lauten.
</section>
<section>
<h2>Provider</h2>
<p>Was ist ein Provider in AngularJS?</p>
<p>
AngularJS Provider (oder Services) sind Singleton Objekte, die spezifische Funktionalitäten zur
Verfügung stellen und Applikationslogik enthalten.
</p>
</section>
<section>
<h2>AngularJS Provider-Typen</h2>
<ul>
<li>Provider</li>
<li>Factory</li>
<li>Service</li>
<li>Value</li>
<li>Constant</li>
</ul>
</section>
<section>
<h2>Provider</h2>
<ul>
<li class="fragment">Parent der anderen Service Typen (ausser Constant).</li>
<li class="fragment">Konfigurierbar.</li>
<li class="fragment">DI Abhängigkeiten im Provider Konstruktor können nur andere Provider oder Constants sein.</li>
<li class="fragment">Beispiele: $stateProvider, $httpProvider.</li>
</ul>
</section>
<section>
<h2>Provider Beispiel</h2>
<pre><code class="javascript" data-trim>
class MyService { /* ... */ }
function MyServiceProvider() {
let timeout = 3000;
this.setTimeout = function(seconds) {
timeout = seconds;
};
this.$get = [function() {
return new MyService(timeout); // will only be called once
}];
}
angular.module('SomeModule').provider('MyService', [MyServiceProvider]);
</code></pre>
</section>
<section>
<h2>Factory</h2>
<p>Wie Provider, aber ohne Konfiguration.</p>
</section>
<section>
<h2>Factory Beispiel</h2>
<pre><code class="javascript" data-trim>
function TestFactory() {
const thisIsPrivate = 'Private';
function getPrivate() {
return thisIsPrivate;
}
return {
variable: 'This is public',
getPrivate: getPrivate
};
}
angular.module('SomeModule').factory('TestFactory', [TestFactory]);
</code></pre>
</section>
<section>
<h2>Service</h2>
<p>Wie Factory, aber anstatt einem Objekt definieren wir eine Klasse, die mit 'new' instanziert wird.</p>
<p><strong>Empfehlung ➾ Services gegenüber Factories vorziehen.</strong></p>
</section>
<section>
<h2>Service Beispiel</h2>
<pre><code class="javascript" data-trim>
class HelloService {
constructor() {
// Do some initialization
}
sayHello() {
console.log('Hello!')
}
}
angular.module('SomeModule').service('HelloService', [HelloService]);
</code></pre>
</section>
<section>
<h2>Value / Constant</h2>
<ul>
<li class="fragment">Ein Value kann alles sein, String, Date, Array, Number oder Object.</li>
<li class="fragment">Ideal um statische Daten zu hinterlegen, welche über mehrere Controller, Services etc. hinweg benötigt werden.</li>
<li class="fragment">Constants sind auch innerhalb von config Funktionen verfügbar, Values nicht. Das ist der einzige Unterschied.</li>
<li class="fragment">Good to know: Constants können trotz des Namens geändert werden.</li>
</ul>
</section>
<section>
<h2>Constant Beispiel</h2>
<pre><code class="javascript" data-trim>
angular
.module('SomeModule')
.constant('apiBaseUrl', 'https://myhost/api/v1/');
// Wert kann auch ein Javascript Objekt sein:
angular
.module('SomeModule')
.constant('apiSettings', {
baseUrl: 'https://myhost/api/',
apiKey: 'letmein'
});
</code></pre>
</section>
<section data-state="hands-on">
<h2>Hands-on</h2>
<ul>
<li>Erstelle einen Service "ContactsService" mit der Methode “fetchContacts” der die Inhalte der Kontaktliste unter <nobr>https://nameless-sierra-8010.herokuapp.com/api/Contacts</nobr> zurückgibt.</li>
<li>Verwende dafür den Angular $http Service.</li>
<li>Ersetze die statische Kontaktliste mit der neuen aus dem Service.</li>
</ul>
<h3>Bonus</h3>
<ul>
<li>Mach den URL zum API-Endpoint konfigurierbar.</li>
</ul>
</section>
<section data-state="hands-on">
<h2>Hands-on</h2>
<ul>
<li>Erweitere den ContactsService um folgende Funktionen:
<li>ContactsService.detailContact(id): Gibt einen bestimmten Kontakt zurück.</li>
<li>ContactsService.createOrUpdateContact(contact): Erstellt oder aktualisiert einen Kontakt, je nachdem ob contact.id definiert ist oder nicht.</li>
<li>Erweitere die Detail-View mit den neuen Service Funktionen</li>
</ul>
</section>
<section>
<h2>Authentication</h2>