-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter9.html
647 lines (606 loc) · 27.9 KB
/
chapter9.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
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Angular, part I</title>
<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 rel="icon" type="image/x-icon" href="mc2/images/favicon.ico">
<link rel="stylesheet" href="mc2/styles/reveal.css">
<link rel="stylesheet" href="mc2/styles/theme.css" id="theme">
<link rel="stylesheet" href="mc2/styles/code.css">
<!--STARTCOURSESPECIFICSTYLES--><link rel="stylesheet" href="styles/cssdemo.css">
<link rel="stylesheet" href="styles/sectiontypes.css">
<link rel="stylesheet" href="styles/style.css"><!--ENDCOURSESPECIFICSTYLES-->
</head>
<body>
<div id="pos"></div>
<div class="reveal">
<div class="slides">
<section class="slide chaptertitle">
<div class="slidecontent">
<div class="chapternumber"> chapter 9 of 11 </div>
<h1>Angular, part I</h1>
<span>The need for which is... acute</span>
</div>
</section>
<!--STARTSECTIONINDEX--><section class="slide sectionlist">
<div class="slidecontent">
<h3>Sections in this chapter</h3>
<ol>
<li><a href="#/3">Deep end ‑ exercise!</a></li>
<li><a href="#/4">Models and views</a></li>
<li><a href="#/5">Exercise</a></li>
<li><a href="#/6">Conditional rendering</a></li>
<li><a href="#/7">Exercise</a></li>
<li><a href="#/8">Repeated rendering</a></li>
<li><a href="#/9">Exercise</a></li>
<li><a href="#/10">Styles</a></li>
<li><a href="#/11">Exercise</a></li>
<li><a href="#/12">Behavior</a></li>
<li><a href="#/13">Exercise</a></li>
<li><a href="#/14">React</a></li>
</ol>
</div>
</section>
<!--ENDSECTIONINDEX-->
<!--STARTCHAPTERCONTENT--><!--STARTSECTION1--><section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 1/12</div>
<h3>Deep end - exercise!</h3>
<p>Whoa!</p>
</div>
</section>
<section class="slide" data-pos="9-1-1">
<span class="pos">9-1-1</span>
<div class="slidecontent"><div class="goal"></div>
<p>Exercise time.</p>
<p>Wait, really? Yes!</p>
</div></section><section class="slide" data-pos="9-1-2">
<span class="pos">9-1-2</span>
<div class="slidecontent"><p>Step 1: Install Angular.</p>
<pre><code class="lang-sh">$ npm i -g @angular/cli
</code></pre>
<p>You may need to do this as Administrator. The tool installed is called <code>ng</code>.</p>
</div></section><section class="slide" data-pos="9-1-3">
<span class="pos">9-1-3</span>
<div class="slidecontent"><p>Step 2: Create your project</p>
<pre><code class="lang-sh">$ <span class="hljs-built_in">cd</span> Documents
$ ng new my-first-project
</code></pre>
</div></section><section class="slide" data-pos="9-1-4">
<span class="pos">9-1-4</span>
<div class="slidecontent"><p>Step 3: inside the project, serve the app</p>
<pre><code class="lang-sh">$ <span class="hljs-built_in">cd</span> my-first-project
$ ng serve --open
</code></pre>
</div></section><section class="slide" data-pos="9-1-5">
<span class="pos">9-1-5</span>
<div class="slidecontent"><div class="checklist"></div>
<p>You are done when...</p>
<ul>
<li>You have your first Angular app up and running!</li>
</ul>
</div></section><section class="slide" data-pos="9-1-6">
<span class="pos">9-1-6</span>
<div class="slidecontent"><p>When you do have the project up and running, feel free to look around in the
project's directory structure.</p>
<p>More information on <a href="https://angular.io">angular.io</a>.</p>
</div></section><section class="slide" data-pos="9-1-7">
<span class="pos">9-1-7</span>
<div class="slidecontent"><p>Stretch tasks: Can you...</p>
<ul>
<li>...remove the Angular logotype?</li>
<li>...change the style of the heading?</li>
<li>...change the value of <em>title</em> in the heading?</li>
<li>...break the whole app in some supremely educational way?</li>
</ul>
</div></section>
</section>
<!--ENDSECTION1-->
<!--STARTSECTION2--><section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 2/12</div>
<h3>Models and views</h3>
<p>And their discontents</p>
</div>
</section>
<section class="slide" data-pos="9-2-1">
<span class="pos">9-2-1</span>
<div class="slidecontent"><div class="learn"></div>
<p>What's a model? What's a view?</p>
<p>And, how we got here.</p>
</div></section><section class="slide" data-pos="9-2-2">
<span class="pos">9-2-2</span>
<div class="slidecontent"><p>Remember the thing with the <code>render()</code> functions?</p>
</div></section><section class="slide" data-pos="9-2-3">
<span class="pos">9-2-3</span>
<div class="slidecontent"><p>Throughout the past decade, we've been struggling with updating the DOM.</p>
</div></section><section class="slide" data-pos="9-2-4">
<span class="pos">9-2-4</span>
<div class="slidecontent"><p>The main reason jQuery is passé.</p>
</div></section><section class="slide" data-pos="9-2-5">
<span class="pos">9-2-5</span>
<div class="slidecontent"><p>What we really want:</p>
<ul>
<li><strong>Model</strong> - the data in the JavaScript</li>
<li><strong>View</strong> - the structure in the HTML</li>
</ul>
<p>When we change the model, the view should change "automatically".</p>
</div></section><section class="slide" data-pos="9-2-6">
<span class="pos">9-2-6</span>
<div class="slidecontent"><p>There are other, bigger, concerns too:</p>
<ul>
<li>Larger-scale architecture (app structure, services...)</li>
<li>Unit testing and end-to-end testing</li>
<li>Routing (we'll come back to that)</li>
<li>Cross-cutting (logging, auth, data storage)</li>
</ul>
</div></section>
</section>
<!--ENDSECTION2-->
<!--STARTSECTION3--><section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 3/12</div>
<h3>Exercise</h3>
<p>It's been N seconds</p>
</div>
</section>
<section class="slide" data-pos="9-3-1">
<span class="pos">9-3-1</span>
<div class="slidecontent"><div class="goal"></div>
<p>Let's implement a Very Small Angular application.</p>
</div></section><section class="slide" data-pos="9-3-2">
<span class="pos">9-3-2</span>
<div class="slidecontent"><pre><code class="lang-typescript"><span class="hljs-keyword">import</span> {Component, OnInit} <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/core'</span>;
<span class="hljs-meta">@Component</span>({ <span class="hljs-comment">/* ... */</span> })
<span class="hljs-keyword">export</span> <span class="hljs-keyword">class</span> AppComponent <span class="hljs-keyword">implements</span> OnInit {
secondsElapsed = <span class="hljs-number">0</span>;
ngOnInit() {
setInterval(<span class="hljs-function"><span class="hljs-params">()</span> =></span> {
<span class="hljs-keyword">this</span>.secondsElapsed++;
}, <span class="hljs-number">1000</span>);
}
}
</code></pre>
</div></section><section class="slide" data-pos="9-3-3">
<span class="pos">9-3-3</span>
<div class="slidecontent"><div class="checklist"></div>
<p>You are done when...</p>
<ul>
<li>The message "This app has been running for 0 seconds" is displayed</li>
<li>The 0 is bolded</li>
<li>The number ticks up every second</li>
</ul>
</div></section>
</section>
<!--ENDSECTION3-->
<!--STARTSECTION4--><section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 4/12</div>
<h3>Conditional rendering</h3>
<p>Neither air conditioning nor hair conditioning</p>
</div>
</section>
<section class="slide" data-pos="9-4-1">
<span class="pos">9-4-1</span>
<div class="slidecontent"><div class="learn"></div>
<p>Learning how to render something... sometimes.</p>
</div></section><section class="slide" data-pos="9-4-2">
<span class="pos">9-4-2</span>
<div class="slidecontent"><p>The syntax for rendering something conditionally is <code>*ngIf</code>.</p>
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-name">div</span> *<span class="hljs-attr">ngIf</span>=<span class="hljs-string">"myProperty"</span>></span>
This will only show if myProperty is truthy!
<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
</code></pre>
<p>Angular will expect you to have declared <code>myProperty</code> on your component.</p>
</div></section><section class="slide question" data-pos="9-4-3">
<span class="pos">9-4-3</span>
<div class="slidecontent">
<p>What does the star in <code>*ngIf</code> mean?</p>
</div></section><section class="slide answer" data-pos="9-4-4">
<span class="pos">9-4-4</span>
<div class="slidecontent">
<p>It means <code>ngIf</code> is not a "normal" property.</p>
<p>Angular takes the <code>ngIf</code> and does something weird/structural with it.</p>
</div></section>
</section>
<!--ENDSECTION4-->
<!--STARTSECTION5--><section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 5/12</div>
<h3>Exercise</h3>
<p>Conditional rendering, here I come!</p>
</div>
</section>
<section class="slide" data-pos="9-5-1">
<span class="pos">9-5-1</span>
<div class="slidecontent"><div class="goal"></div>
<p>Let's use conditional rendering for something!</p>
</div></section><section class="slide" data-pos="9-5-2">
<span class="pos">9-5-2</span>
<div class="slidecontent"><div class="checklist"></div>
<p>You are done when...</p>
<ul>
<li>Your app component has a boolean called <code>visible</code> which starts at true</li>
<li>There are two different divs in your app</li>
<li>One says "Now you see me"</li>
<li>The other says "(now you don't)"</li>
<li>Only one of them at a time is visible</li>
<li>They toggle between each other every three seconds</li>
</ul>
</div></section><section class="slide" data-pos="9-5-3">
<span class="pos">9-5-3</span>
<div class="slidecontent"><p>Stretch tasks:</p>
<ul>
<li>Try styling the divs nicely</li>
<li>Can you also make the divs move downwards across the page? (This may take some searching.)</li>
</ul>
</div></section>
</section>
<!--ENDSECTION5-->
<!--STARTSECTION6--><section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 6/12</div>
<h3>Repeated rendering</h3>
<p>Again and again and again...</p>
</div>
</section>
<section class="slide" data-pos="9-6-1">
<span class="pos">9-6-1</span>
<div class="slidecontent"><div class="learn"></div>
<p>Learning how to repeat a given element over an array of data.</p>
</div></section><section class="slide" data-pos="9-6-2">
<span class="pos">9-6-2</span>
<div class="slidecontent"><p>Just like we have <code>*ngIf</code>, we also have <code>*ngFor</code>. It's modeled on ES6 <code>for</code>
loops.</p>
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-name">table</span>></span>
<span class="hljs-tag"><<span class="hljs-name">tr</span> *<span class="hljs-attr">ngFor</span>=<span class="hljs-string">"let row of rows"</span>></span>
<span class="hljs-comment"><!-- ... --></span>
<span class="hljs-tag"></<span class="hljs-name">tr</span>></span>
<span class="hljs-tag"></<span class="hljs-name">table</span>></span>
</code></pre>
<p>Again, you're expected to have declared a <code>rows</code> property.</p>
</div></section>
</section>
<!--ENDSECTION6-->
<!--STARTSECTION7--><section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 7/12</div>
<h3>Exercise</h3>
<p>Loop the loop!</p>
</div>
</section>
<section class="slide" data-pos="9-7-1">
<span class="pos">9-7-1</span>
<div class="slidecontent"><div class="goal"></div>
<p>Let's render a list of things!</p>
</div></section><section class="slide" data-pos="9-7-2">
<span class="pos">9-7-2</span>
<div class="slidecontent"><div class="checklist"></div>
<p>You are done when...</p>
<ul>
<li>Your app component has a field <code>favorites</code> with an array of strings</li>
<li>The strings are of your favorite sauces, or superheroes, or fountain pens</li>
<li>You have rendered these out as an <code>ul</code> list on the page</li>
</ul>
</div></section><section class="slide" data-pos="9-7-3">
<span class="pos">9-7-3</span>
<div class="slidecontent"><p>As a stretch task, turn the strings in the array into objects with an
<code>id: number</code> and a <code>description: string</code>. Make the app work that way instead.</p>
<p>Also as a stretch task, see if you can <code>setTimeout</code> in the <code>ngOnInit()</code> method,
adding a new element to the list a few seconds after the page has loaded.</p>
</div></section>
</section>
<!--ENDSECTION7-->
<!--STARTSECTION8--><section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 8/12</div>
<h3>Styles</h3>
<p>Not to mention class</p>
</div>
</section>
<section class="slide" data-pos="9-8-1">
<span class="pos">9-8-1</span>
<div class="slidecontent"><div class="learn"></div>
<p>Learning how to set <em>dynamic</em> styles.</p>
</div></section><section class="slide" data-pos="9-8-2">
<span class="pos">9-8-2</span>
<div class="slidecontent"><p>For every normal attribute:</p>
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"..."</span> <span class="hljs-attr">title</span>=<span class="hljs-string">"you can see me when you hover"</span>></span>
</code></pre>
<p>You have the option to <em>bind</em> some property to it instead:</p>
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"..."</span> <span class="hljs-attr">bind-title</span>=<span class="hljs-string">"dynamicTitle"</span>></span>
</code></pre>
</div></section><section class="slide" data-pos="9-8-3">
<span class="pos">9-8-3</span>
<div class="slidecontent"><p>Note this is different from the <code>{{ dynamicTitle }}</code> syntax!</p>
<p>That syntax only works with text nodes. With attributes you have to <code>bind-</code></p>
</div></section><section class="slide" data-pos="9-8-4">
<span class="pos">9-8-4</span>
<div class="slidecontent"><p>Good news! There's a shorthand syntax. Instead of:</p>
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"..."</span> <span class="hljs-attr">bind-title</span>=<span class="hljs-string">"dynamicTitle"</span>></span>
</code></pre>
<p>You can instead do:</p>
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"..."</span> [<span class="hljs-attr">title</span>]=<span class="hljs-string">"dynamicTitle"</span>></span>
</code></pre>
<p>Entirely synonymous. The box looks weird at first, but it grows on you.</p>
</div></section><section class="slide" data-pos="9-8-5">
<span class="pos">9-8-5</span>
<div class="slidecontent"><p>For style and class attributes, there are further shorthands:</p>
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-name">div</span> [<span class="hljs-attr">style.background</span>]=<span class="hljs-string">"dynamicBackground"</span>></span>...<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
<span class="hljs-tag"><<span class="hljs-name">div</span> [<span class="hljs-attr">class.highlight</span>]=<span class="hljs-string">"isItHighlighted"</span>></span>...<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
</code></pre>
</div></section>
</section>
<!--ENDSECTION8-->
<!--STARTSECTION9--><section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 9/12</div>
<h3>Exercise</h3>
<p>Setting styles!</p>
</div>
</section>
<section class="slide" data-pos="9-9-1">
<span class="pos">9-9-1</span>
<div class="slidecontent"><div class="goal"></div>
<p>Let's use our newly learned skills and set some styles on some things.</p>
</div></section><section class="slide" data-pos="9-9-2">
<span class="pos">9-9-2</span>
<div class="slidecontent"><div class="checklist"></div>
<p>You are done when you have...</p>
<ul>
<li>A div that cycles through three background colors at regular interval</li>
<li>A h1 heading that changes between two styles, preferably by setting/unsetting a class</li>
</ul>
</div></section>
</section>
<!--ENDSECTION9-->
<!--STARTSECTION10--><section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 10/12</div>
<h3>Behavior</h3>
<p>If it moves, it's biology</p>
</div>
</section>
<section class="slide" data-pos="9-10-1">
<span class="pos">9-10-1</span>
<div class="slidecontent"><div class="learn"></div>
<p>Learning to listen to events and declare event methods.</p>
</div></section><section class="slide" data-pos="9-10-2">
<span class="pos">9-10-2</span>
<div class="slidecontent"><p>Just like there is <code>bind-</code>, there is also <code>on-</code>.</p>
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-name">button</span> <span class="hljs-attr">on-click</span>=<span class="hljs-string">"addProduct(product)"</span>></span>Add<span class="hljs-tag"></<span class="hljs-name">button</span>></span>
</code></pre>
<p><code>addProduct()</code> is expected to be a method on the component class.</p>
</div></section><section class="slide" data-pos="9-10-3">
<span class="pos">9-10-3</span>
<div class="slidecontent"><p>Shorthand! Parentheses. What you can write like this:</p>
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-name">button</span> <span class="hljs-attr">on-click</span>=<span class="hljs-string">"addProduct(product)"</span>></span>Add<span class="hljs-tag"></<span class="hljs-name">button</span>></span>
</code></pre>
<p>You can also write like this:</p>
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-name">button</span> (<span class="hljs-attr">click</span>)=<span class="hljs-string">"addProduct(product)"</span>></span>Add<span class="hljs-tag"></<span class="hljs-name">button</span>></span>
</code></pre>
</div></section><section class="slide" data-pos="9-10-4">
<span class="pos">9-10-4</span>
<div class="slidecontent"><p>Declaring a method is straightforward:</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">class</span> ShoppingCart {
products: Product[] = [];
addProduct(product) {
<span class="hljs-keyword">this</span>.products.push(product);
}
}
</code></pre>
</div></section><section class="slide" data-pos="9-10-5">
<span class="pos">9-10-5</span>
<div class="slidecontent"><p>There's a special variable <code>$event</code> if you need details of the event.</p>
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-name">div</span> (<span class="hljs-attr">mousemove</span>)=<span class="hljs-string">"displayCoordinates($event)"</span>></span>
Move the mouse pointer over me!
<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
</code></pre>
</div></section><section class="slide" data-pos="9-10-6">
<span class="pos">9-10-6</span>
<div class="slidecontent"><p>More on which later, but it's also useful to know about the <code>#id</code> syntax:</p>
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-name">input</span> #<span class="hljs-attr">field</span>></span>
<span class="hljs-tag"><<span class="hljs-name">button</span> (<span class="hljs-attr">click</span>)=<span class="hljs-string">"submit(field.value)"</span>></span>Submit<span class="hljs-tag"></<span class="hljs-name">button</span>></span>
</code></pre>
<p>It's a <em>template-only</em> way to identify DOM nodes.</p>
</div></section>
</section>
<!--ENDSECTION10-->
<!--STARTSECTION11--><section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 11/12</div>
<h3>Exercise</h3>
<p>Behavior time!</p>
</div>
</section>
<section class="slide" data-pos="9-11-1">
<span class="pos">9-11-1</span>
<div class="slidecontent"><div class="goal"></div>
<p>Now we have enough to build a "words of wisdom" application!</p>
</div></section><section class="slide" data-pos="9-11-2">
<span class="pos">9-11-2</span>
<div class="slidecontent"><p>Your template can start out something like this:</p>
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-name">div</span>></span>
<span class="hljs-tag"><<span class="hljs-name">ul</span>></span>
<span class="hljs-tag"><<span class="hljs-name">li</span> *<span class="hljs-attr">ngFor</span>=<span class="hljs-string">"let item of items"</span>></span>
{{item.description}}
<span class="hljs-tag"></<span class="hljs-name">li</span>></span>
<span class="hljs-tag"></<span class="hljs-name">ul</span>></span>
<span class="hljs-tag"><<span class="hljs-name">input</span> #<span class="hljs-attr">new</span>></span>
<span class="hljs-tag"><<span class="hljs-name">button</span>></span>Add<span class="hljs-tag"></<span class="hljs-name">button</span>></span>
<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
</code></pre>
</div></section><section class="slide" data-pos="9-11-3">
<span class="pos">9-11-3</span>
<div class="slidecontent"><div class="checklist"></div>
<p>You are done when...</p>
<ul>
<li>The list shows a few words of wisdom at the start, such as "carpe diem"</li>
<li>You can press the button and a new one is added</li>
<li>...and the input is cleared</li>
</ul>
</div></section><section class="slide" data-pos="9-11-4">
<span class="pos">9-11-4</span>
<div class="slidecontent"><p>Stretch task: See if you can add Bootstrap styling to your app!</p>
</div></section>
</section>
<!--ENDSECTION11-->
<!--STARTSECTION12--><section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 12/12</div>
<h3>React</h3>
<p>The other white meat</p>
</div>
</section>
<section class="slide" data-pos="9-12-1">
<span class="pos">9-12-1</span>
<div class="slidecontent"><div class="learn"></div>
<p>Learning a bit about Angular's biggest rival, React.</p>
</div></section><section class="slide" data-pos="9-12-2">
<span class="pos">9-12-2</span>
<div class="slidecontent"><p>Installing:</p>
<pre><code class="lang-sh">$ npm install -g create-react-app
</code></pre>
<p>(As Administrator.)</p>
</div></section><section class="slide" data-pos="9-12-3">
<span class="pos">9-12-3</span>
<div class="slidecontent"><p>Creating a new project:</p>
<pre><code class="lang-sh">$ create-react-app my-react-app
</code></pre>
</div></section><section class="slide" data-pos="9-12-4">
<span class="pos">9-12-4</span>
<div class="slidecontent"><p>React starts completely from the JavaScript end.</p>
<p>It asks you to think of a component (and the entire app) as a <em>function</em>
from the model to the view.</p>
</div></section><section class="slide" data-pos="9-12-5">
<span class="pos">9-12-5</span>
<div class="slidecontent"><p>Each component is its own "render function":</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> App = <span class="hljs-function">(<span class="hljs-params">props</span>) =></span> (
<span class="xml"><span class="hljs-tag"><<span class="hljs-name">div</span>></span>
Hello, { props.name || "World" }
<span class="hljs-tag"></<span class="hljs-name">div</span>></span></span>
);
</code></pre>
<p>Mixing of HTML into the JavaScript: JSX.</p>
</div></section><section class="slide" data-pos="9-12-6">
<span class="pos">9-12-6</span>
<div class="slidecontent"><p>Conceptual purity: re-render the whole app every time.</p>
<p>Various optimizations make this realistic.</p>
</div></section><section class="slide" data-pos="9-12-7">
<span class="pos">9-12-7</span>
<div class="slidecontent"><p>There's also class-based components.</p>
<pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">App</span> </span>{
render() {
<span class="hljs-keyword">return</span> (
<span class="xml"><span class="hljs-tag"><<span class="hljs-name">div</span>></span>
Hello, { props.name || "World" }
<span class="hljs-tag"></<span class="hljs-name">div</span>></span></span>
);
}
}
</code></pre>
</div></section><section class="slide question" data-pos="9-12-8">
<span class="pos">9-12-8</span>
<div class="slidecontent">
<p>Why would you ever want to prefer the class components to the
Purely Functional Components?</p>
</div></section><section class="slide answer" data-pos="9-12-9">
<span class="pos">9-12-9</span>
<div class="slidecontent">
<p>Two reasons have evolved:</p>
<ul>
<li>To keep internal/private state</li>
<li>To do things with lifecycle hooks</li>
</ul>
</div></section><section class="slide" data-pos="9-12-10">
<span class="pos">9-12-10</span>
<div class="slidecontent"><p>Historically:</p>
<ul>
<li>AngularJS (2009)</li>
<li>React (2013)</li>
<li>Angular (2016)</li>
</ul>
</div></section><section class="slide" data-pos="9-12-11">
<span class="pos">9-12-11</span>
<div class="slidecontent"><p>Time to play around with React?</p>
</div></section>
</section>
<!--ENDSECTION12--><!--ENDCHAPTERCONTENT-->
</div>
</div>
<script type="text/javascript">
var basehref = window.location.href.replace(/chapter\d.*?$/,'')
document.addEventListener("keydown",function(e){
var code = e.which || e.keyCode;
if (code===13){
window.location.href = basehref+"index.html?from="+(9-1);
} else if (code >= 49 && code <= 12+48) {
window.location.hash = "#/" + (code-48+1+1);
}
if (9 < 11) {
if (code===99){ // the letter C for next Chapter
window.location.href = basehref+"chapter"+(9+1)+".html"
}
}
});
</script>
<script src="mc2/scripts/head.js" type="text/javascript"></script>
<script src="mc2/scripts/reveal.js" type="text/javascript"></script>
<script src="mc2/scripts/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
showNotes: false,
transition: 'slide',
dependencies: [
{ src: 'mc2/scripts/plugin/notes/notes.js', async: true }
]
})
window.onload = function(){
var links = document.querySelectorAll("a.link");
for(var i=0;i<links.length;i++){
var link = links[i];
link.innerHTML = link.innerHTML.replace(/ /g,' ')
}
var posElem = document.getElementById('pos')
function updateReference(){
setTimeout(function(){
var currentpos = document.querySelector('section.present[data-pos]')
if (currentpos){
posElem.innerHTML = currentpos.getAttribute('data-pos')
} else {
posElem.innerHTML = ''
}
if (document.querySelector('.present.chaptertitle')){
document.body.classList.add('atchaptertitle');
} else {
document.body.classList.remove('atchaptertitle');
}
},10)
}
window.addEventListener("hashchange",updateReference);
updateReference();
};
</script>
<!--STARTCOURSESPECIFICSCRIPTS--><script type="text/javascript" src="scripts/cssdemo.js"></script>
<script type="text/javascript" src="scripts/scoped_polyfill.js"></script><!--ENDCOURSESPECIFICSCRIPTS-->
</body>
</html>