0% found this document useful (0 votes)
5 views65 pages

12373

The document promotes various ebooks related to Angular, including titles like 'The Complete Book on Angular 2' and 'ng-book: The Complete Guide to Angular'. It provides links to download these ebooks in multiple formats such as PDF, ePub, and MOBI from textbookfull.com. Additionally, it highlights the ease of access to digital content for readers on any device.

Uploaded by

rhonaserwi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views65 pages

12373

The document promotes various ebooks related to Angular, including titles like 'The Complete Book on Angular 2' and 'ng-book: The Complete Guide to Angular'. It provides links to download these ebooks in multiple formats such as PDF, ePub, and MOBI from textbookfull.com. Additionally, it highlights the ease of access to digital content for readers on any device.

Uploaded by

rhonaserwi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 65

Explore the full ebook collection and download it now at textbookfull.

com

Ang-book 2: The Complete Book on Angular 2 1st


Edition Nate Murray

https://textbookfull.com/product/ang-book-2-the-complete-
book-on-angular-2-1st-edition-nate-murray/

OR CLICK HERE

DOWLOAD EBOOK

Browse and Get More Ebook Downloads Instantly at https://textbookfull.com


Click here to visit textbookfull.com and download textbook now
Your digital treasures (PDF, ePub, MOBI) await
Download instantly and pick your perfect format...

Read anywhere, anytime, on any device!

ng-book The Complete Guide to Angular Nate Murray

https://textbookfull.com/product/ng-book-the-complete-guide-to-
angular-nate-murray/

textbookfull.com

ng-book: The Complete Guide to Angular 5 Nathan Murray

https://textbookfull.com/product/ng-book-the-complete-guide-to-
angular-5-nathan-murray/

textbookfull.com

Ng Book The Complete Guide to Angular 9 Nathan Murray

https://textbookfull.com/product/ng-book-the-complete-guide-to-
angular-9-nathan-murray/

textbookfull.com

Ng Book The Complete Guide to Angular 11 Felipe Coury

https://textbookfull.com/product/ng-book-the-complete-guide-to-
angular-11-felipe-coury/

textbookfull.com
ng book The Complete Book on AngularJS 1st Edition Ari
Lerner

https://textbookfull.com/product/ng-book-the-complete-book-on-
angularjs-1st-edition-ari-lerner/

textbookfull.com

Angular 2 Cookbook Frisbie

https://textbookfull.com/product/angular-2-cookbook-frisbie/

textbookfull.com

Angular 2 ui development Kasagoni

https://textbookfull.com/product/angular-2-ui-development-kasagoni/

textbookfull.com

Marquise (Mansion On The Hill Book 2) 1st Edition


Chashiree M. & M.K. Moore [M.

https://textbookfull.com/product/marquise-mansion-on-the-hill-
book-2-1st-edition-chashiree-m-m-k-moore-m/

textbookfull.com

Kovana Outsider Book 2 1st Edition Aiden Phoenix

https://textbookfull.com/product/kovana-outsider-book-2-1st-edition-
aiden-phoenix/

textbookfull.com
ng-book 2
Felipe Coury, Ari Lerner, Nate Murray, & Carlos Taborda
© 2015 - 2016 Felipe Coury, Ari Lerner, Nate Murray, & Carlos Taborda
Contents

Book Revision . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Prerelease . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Bug Reports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Chat With The Community! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Be notified of updates via Twitter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
We’d love to hear from you! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

Writing your First Angular 2 Web Application . . . . . . . . . . . . . . . . . . . . . . . . 1


Simple Reddit Clone . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
TypeScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Example Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Angular’s Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
All Dependencies Loaded . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Adding CSS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Our First TypeScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Making a Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Adding a template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Booting Our Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Loading our Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Running The App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Compiling the TypeScript Code to .js . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Using npm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Serving The App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Compiling on every change . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Adding Data to the Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Working with arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Expanding our Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
The Application Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Adding Interaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Adding the Article Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Rendering Multiple Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Creating an Article class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Storing multiple Articles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
CONTENTS

Configuring the ArticleComponent with inputs . . . . . . . . . . . . . . . . . . . . . 46


Rendering a List of Articles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Adding New Articles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Finishing Touches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Displaying the Article Domain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Re-sorting Based on Score . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Full Code Listing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Wrapping Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Getting Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

TypeScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Angular 2 is built in TypeScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
What do we get with TypeScript? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Trying it out with a REPL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Built-in types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Fat Arrow Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Template Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Wrapping up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74

How Angular Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75


Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
The Navigation Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
The Breadcrumbs Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
The Product List Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Product Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Component Decorator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Component selector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Component template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Adding A Product . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Viewing the Product with Template Binding . . . . . . . . . . . . . . . . . . . . . . . 85
Adding More Products . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Selecting a Product . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Listing products using <products-list> . . . . . . . . . . . . . . . . . . . . . . . . . 87
The ProductsList Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Configuring the ProductsList @Component Options . . . . . . . . . . . . . . . . . . . 91
CONTENTS

Component inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Component outputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Emitting Custom Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Writing the ProductsList Controller Class . . . . . . . . . . . . . . . . . . . . . . . . 97
Writing the ProductsList View Template . . . . . . . . . . . . . . . . . . . . . . . . 98
The Full ProductsList Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
The ProductRow Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
ProductRow Component Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . 103
ProductRow Component Definition Class . . . . . . . . . . . . . . . . . . . . . . . . . 103
ProductRow template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
ProductRow Full Listing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
The ProductImage Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
The PriceDisplay Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
The ProductDepartment Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
NgModule and Booting the App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Booting the app . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
The Completed Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
A Word on Data Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110

Built-in Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111


Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
NgIf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
NgSwitch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
NgStyle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
NgClass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
NgFor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Getting an index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
NgNonBindable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126

Forms in Angular 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127


Forms are Crucial, Forms are Complex . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
FormControls and FormGroups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
FormControl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
FormGroup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Our First Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Loading the FormsModule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Simple SKU Form: @Component Annotation . . . . . . . . . . . . . . . . . . . . . . . 131
Simple SKU Form: template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Simple SKU Form: Component Definition Class . . . . . . . . . . . . . . . . . . . . . 135
Try it out! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Using FormBuilder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Reactive Forms with FormBuilder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
CONTENTS

Using FormBuilder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138


Using myForm in the view . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Try it out! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Adding Validations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Explicitly setting the sku FormControl as an instance variable . . . . . . . . . . . . . 143
Custom Validations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Watching For Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
ngModel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Wrapping Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153

HTTP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Using @angular/http . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
import from @angular/http . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
A Basic Request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Building the SimpleHTTPComponent @Component . . . . . . . . . . . . . . . . . . . . . 157
Building the SimpleHTTPComponent template . . . . . . . . . . . . . . . . . . . . . . . 157
Building the SimpleHTTPComponent Controller . . . . . . . . . . . . . . . . . . . . . . 158
Full SimpleHTTPComponent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Writing a YouTubeSearchComponent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Writing a SearchResult . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Writing the YouTubeService . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Writing the SearchBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Writing SearchResultComponent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Writing YouTubeSearchComponent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
@angular/http API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Making a POST request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
PUT / PATCH / DELETE / HEAD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
RequestOptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186

Routing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Why Do We Need Routing? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
How client-side routing works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
The beginning: using anchor tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
The evolution: HTML5 client-side routing . . . . . . . . . . . . . . . . . . . . . . . . 189
Writing our first routes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Components of Angular 2 routing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Imports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Routes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Installing our Routes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
RouterOutlet using <router-outlet> . . . . . . . . . . . . . . . . . . . . . . . . . . 193
RouterLink using [routerLink] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
CONTENTS

Putting it all together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195


Creating the Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
HomeComponent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
AboutComponent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
ContactComponent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Application Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
Configuring the Routes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Routing Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Path location strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Running the application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Route Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
ActivatedRoute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
Music Search App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
First Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
The SpotifyService . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
The SearchComponent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Trying the search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
TrackComponent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
Wrapping up music search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Router Hooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
AuthService . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
LoginComponent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
ProtectedComponent and Route Guards . . . . . . . . . . . . . . . . . . . . . . . . . . 229
Nested Routes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Configuring Routes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
ProductsComponent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241

Dependency Injection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242


Injections Example: PriceService . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
“Don’t Call Us…” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Dependency Injection Parts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Playing with an Injector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Providing Dependencies with NgModule . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Using a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Using a Factory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Using a Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
Using an alias . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
Dependency Injection in Apps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
Working with Injectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Substituting values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
NgModule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
CONTENTS

NgModule vs. JavaScript Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267


The Compiler and Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
Dependency Injection and Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
Component Visibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Specifying Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272

Data Architecture in Angular 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273


An Overview of Data Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
Data Architecture in Angular 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274

Data Architecture with Observables - Part 1: Services . . . . . . . . . . . . . . . . . . . . 275


Observables and RxJS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
Note: Some RxJS Knowledge Required . . . . . . . . . . . . . . . . . . . . . . . . . . 275
Learning Reactive Programming and RxJS . . . . . . . . . . . . . . . . . . . . . . . . 275
Chat App Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Implementing the Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
User . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
Implementing UserService . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
currentUser stream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Setting a new user . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
UserService.ts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
The MessagesService . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
the newMessages stream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
the messages stream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
The Operation Stream Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
Sharing the Stream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
Adding Messages to the messages Stream . . . . . . . . . . . . . . . . . . . . . . . . . 291
Our completed MessagesService . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
Trying out MessagesService . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
The ThreadsService . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
A map of the current set of Threads (in threads) . . . . . . . . . . . . . . . . . . . . . 299
A chronological list of Threads, newest-first (in orderedthreads) . . . . . . . . . . . . 304
The currently selected Thread (in currentThread) . . . . . . . . . . . . . . . . . . . . 304
The list of Messages for the currently selected Thread (in currentThreadMessages) . . 306
Our Completed ThreadsService . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
Data Model Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
CONTENTS

Data Architecture with Observables - Part 2: View Components . . . . . . . . . . . . . . 312


Building Our Views: The ChatApp Top-Level Component . . . . . . . . . . . . . . . . . . 312
The ChatThreads Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
ChatThreads Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
ChatThreads template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
The Single ChatThread Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
ChatThread Controller and ngOnInit . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
ChatThread template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
ChatThread Complete Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
The ChatWindow Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
The ChatMessage Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
Setting incoming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
The ChatMessage template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
The Complete ChatMessage Code Listing . . . . . . . . . . . . . . . . . . . . . . . . . 334
The ChatNavBar Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
The ChatNavBar @Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
The ChatNavBar Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
The ChatNavBar template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
The Completed ChatNavBar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
Next Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344

Introduction to Redux with TypeScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345


Redux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
Redux: Key Ideas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
Core Redux Ideas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
What’s a reducer? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Defining Action and Reducer Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . 348
Creating Our First Reducer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
Running Our First Reducer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
Adjusting the Counter With actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Reducer switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
Action “Arguments” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
Storing Our State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
Using the Store . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
Being Notified with subscribe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
The Core of Redux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
A Messaging App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
Messaging App state . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
Messaging App actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
Messaging App reducer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
Trying Out Our Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
Action Creators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
CONTENTS

Using Real Redux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368


Using Redux in Angular . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Planning Our App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Setting Up Redux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Defining the Application State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
Defining the Reducers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
Defining Action Creators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
Creating the Store . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
CounterApp Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
Providing the Store . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
Bootstrapping the App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
The CounterComponent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
imports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
The template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
The constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
What’s Next . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384

Intermediate Redux in Angular . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386


Context For This Chapter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
Chat App Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
Reducers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
Implementing the Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
User . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
App State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
A Word on Code Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
The Root Reducer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
The UsersState . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
The ThreadsState . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
Visualizing Our AppState . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
Building the Reducers (and Action Creators) . . . . . . . . . . . . . . . . . . . . . . . . . 396
Set Current User Action Creators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
UsersReducer - Set Current User . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
Thread and Messages Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
Adding a New Thread Action Creators . . . . . . . . . . . . . . . . . . . . . . . . . . 398
Adding a New Thread Reducer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
Adding New Messages Action Creators . . . . . . . . . . . . . . . . . . . . . . . . . . 400
CONTENTS

Adding A New Message Reducer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401


Selecting A Thread Action Creators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
Selecting A Thread Reducer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
Reducers Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
Building the Angular Chat App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
The top-level ChatApp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
The ChatPage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
Container vs. Presentational Components . . . . . . . . . . . . . . . . . . . . . . . . . 409
Building the ChatNavBar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
Redux Selectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
Threads Selectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
Unread Messages Count Selector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
Building the ChatThreads Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
ChatThreads Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416
ChatThreads template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
The Single ChatThread Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
ChatThread @Component and template . . . . . . . . . . . . . . . . . . . . . . . . . . 419
Building the ChatWindow Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
The ChatMessage Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
Setting incoming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
The ChatMessage template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430

Advanced Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432


Styling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432
View (Style) Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434
Shadow DOM Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
No Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
Creating a Popup - Referencing and Modifying Host Elements . . . . . . . . . . . . . . . 443
Popup Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
Using ElementRef . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
Binding to the host . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
Adding a Button using exportAs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
Creating a Message Pane with Transclusion . . . . . . . . . . . . . . . . . . . . . . . . . 452
Changing the host CSS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
Using ng-content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
Querying Neighbor Directives - Writing Tabs . . . . . . . . . . . . . . . . . . . . . . . . 455
Tab Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
Tabset Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
Using the Tabset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458
Lifecycle Hooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
OnInit and OnDestroy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
OnChanges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
CONTENTS

DoCheck . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471
AfterContentInit, AfterViewInit, AfterContentChecked and AfterViewChecked . . . . 484
Advanced Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
Rewriting ngIf - ngBookIf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
Rewriting ngFor - ngBookRepeat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
Change Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501
Customizing Change Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505
Zones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512
Observables and OnPush . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517

Testing . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 518
Test driven? . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 518
End-to-end vs. Unit Testing . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 518
Testing Tools . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
Jasmine . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
Karma . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 520
Writing Unit Tests . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 520
Angular Unit testing framework . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 520
Setting Up Testing . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 521
Testing Services and HTTP . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 524
HTTP Considerations . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 525
Stubs . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 525
Mocks . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 526
Http MockBackend . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 527
TestBed.configureTestingModule and Providers . . . . . . . . . . . . . . . . . . . . 527
Testing getTrack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528
Testing Routing to Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 534
Creating a Router for Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535
Mocking dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538
Spies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538
Back to Testing Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541
fakeAsync and advance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543
inject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544
Testing ArtistComponent’s Initialization . . . . . . . . . . . . . . . . . . . . . . . . . 544
Testing ArtistComponent Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546
Testing ArtistComponent DOM Template Values . . . . . . . . . . . . . . . . . . . . . 547
Testing Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 550
Creating a ConsoleSpy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552
Installing the ConsoleSpy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553
Configuring the Testing Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554
Testing The Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554
Refactoring Our Form Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557
CONTENTS

Testing HTTP requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 560


Testing a POST . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561
Testing DELETE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 562
Testing HTTP Headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563
Testing YouTubeService . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 565
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 571

Converting an Angular 1 App to Angular 2 . . . . . . . . . . . . . . . . . . . . . . . . . . 572


Peripheral Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 572
What We’re Building . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573
Mapping Angular 1 to Angular 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574
Requirements for Interoperability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576
The Angular 1 App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576
The ng1-app HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578
Code Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579
ng1: PinsService . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579
ng1: Configuring Routes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581
ng1: HomeController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582
ng1: / HomeController template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582
ng1: pin Directive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583
ng1: pin Directive template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583
ng1: AddController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585
ng1: AddController template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587
ng1: Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590
Building A Hybrid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590
Hybrid Project Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590
Bootstrapping our Hybrid App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595
What We’ll Upgrade . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596
A Minor Detour: Typing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599
Writing ng2 PinControlsComponent . . . . . . . . . . . . . . . . . . . . . . . . . . . . 605
Using ng2 PinControlsComponent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 606
Downgrading ng2 PinControlsComponent to ng1 . . . . . . . . . . . . . . . . . . . . 607
Adding Pins with ng2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 609
Upgrading ng1 PinsService and $state to ng2 . . . . . . . . . . . . . . . . . . . . . 611
Writing ng2 AddPinComponent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 612
Using AddPinComponent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 618
Exposing an ng2 service to ng1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 618
Writing the AnalyticsService . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 619
Downgrade ng2 AnalyticsService to ng1 . . . . . . . . . . . . . . . . . . . . . . . . 619
Using AnalyticsService in ng1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622
CONTENTS

Changelog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623
Revision 39 - 2016-09-03 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623
Revision 38 - 2016-08-29 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623
Revision 37 - 2016-08-02 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623
Revision 36 - 2016-07-20 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623
Revision 35 - 2016-06-30 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623
Revision 34 - 2016-06-15 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 624
Revision 33 - 2016-05-11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 624
Revision 32 - 2016-05-06 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 624
Revision 31 - 2016-04-28 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
Revision 30 - 2016-04-20 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
Revision 29 - 2016-04-08 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
Revision 28 - 2016-04-01 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
Revision 27 - 2016-03-25 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
Revision 26 - 2016-03-24 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
Revision 25 - 2016-03-21 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
Revision 24 - 2016-03-10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
Revision 23 - 2016-03-04 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
Revision 22 - 2016-02-24 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 626
Revision 21 - 2016-02-20 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 626
Revision 20 - 2016-02-11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 626
Revision 19 - 2016-02-04 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 627
Revision 18 - 2016-01-29 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 627
Revision 17 - 2016-01-28 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 627
Revision 16 - 2016-01-14 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 627
Revision 15 - 2016-01-07 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 627
Revision 14 - 2015-12-23 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 628
Revision 13 - 2015-12-17 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 628
Revision 12 - 2015-11-16 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 628
Revision 11 - 2015-11-09 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 629
Revision 10 - 2015-10-30 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630
Revision 9 - 2015-10-15 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630
Revision 8 - 2015-10-08 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630
Revision 7 - 2015-09-23 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630
Revision 6 - 2015-08-28 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630
Revision 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630
Revision 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631
Revision 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631
Revision 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631
Revision 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631
CONTENTS 1

Book Revision
Revision 39 - Covers up to Angular 2 (2.0.0-rc.6, 2016-09-03)

Prerelease
This book is a prerelease version and a work-in-progress.

Bug Reports
If you’d like to report any bugs, typos, or suggestions just email us at: us@fullstack.io¹.

Chat With The Community!


We’re experimenting with a community chat room for this book using Gitter. If you’d like to hang
out with other people learning Angular 2, come join us on Gitter²!

Be notified of updates via Twitter


If you’d like to be notified of updates to the book on Twitter, follow @fullstackio³

We’d love to hear from you!


Did you like the book? Did you find it helpful? We’d love to add your face to our list of testimonials
on the website! Email us at: us@fullstack.io⁴.
¹mailto:us@fullstack.io?Subject=ng-book%202%20feedback
²https://gitter.im/ng-book/ng-book
³https://twitter.com/fullstackio
⁴mailto:us@fullstack.io?Subject=ng-book%202%20testimonial
CONTENTS 2


⁵mailto:carlos@ng-book.com
Writing your First Angular 2 Web
Application
Simple Reddit Clone
In this chapter we’re going to build an application that allows the user to post an article (with a
title and a URL) and then vote on the posts.
You can think of this app as the beginnings of a site like Reddit⁶ or Product Hunt⁷.
In this simple app we’re going to cover most of the essentials of Angular 2 including:

• Building custom components


• Accepting user input from forms
• Rendering lists of objects into views
• Intercepting user clicks and acting on them

By the time you’re finished with this chapter you’ll have a good grasp on how to build basic Angular
2 applications.
Here’s a screenshot of what our app will look like when it’s done:
⁶http://reddit.com
⁷http://producthunt.com
Writing your First Angular 2 Web Application 2

Completed application

First, a user will submit a new link and after submitting the users will be able to upvote or downvote
each article. Each link will have a score and we can vote on which links we find useful.
Writing your First Angular 2 Web Application 3

App with new article

In this project, and throughout the book, we’re going to use TypeScript. TypeScript is a superset of
JavaScript ES6 that adds types. We’re not going to talk about TypeScript in depth in this chapter, but
if you’re familiar with ES5 (“normal” javascript) / ES6 (ES2015) you should be able to follow along
without any problems.
We’ll go over TypeScript more in depth in the next chapter. So don’t worry if you’re having
trouble with some of the new syntax.
Writing your First Angular 2 Web Application 4

Getting started

TypeScript
To get started with TypeScript, you’ll need to have Node.js installed. There are a couple of different
ways you can install Node.js, so please refer to the Node.js website for detailed information:
https://nodejs.org/download/⁸ .

Do I have to use TypeScript? No, you don’t have to use TypeScript to use Angular 2, but
you probably should. ng2 does have an ES5 API, but Angular 2 is written in TypeScript
and generally that’s what everyone is using. We’re going to use TypeScript in this book
because it’s great and it makes working with Angular 2 easier. That said, it isn’t strictly
required.

Once you have Node.js setup, the next step is to install TypeScript. Make sure you install at least
version 1.7 or greater. To install it, run the following npm command:

1 $ npm install -g 'typescript@1.9.0-dev.20160409'

npm is installed as part of Node.js. If you don’t have npm on your system, make sure you
used a Node.js installer that includes it.

Windows Users: We’ll be using Linux/Mac-style commands on the commandline through-


out this book. We’d highly recommend you install Cygwin⁹ as it will let you run commands
just as we have them written out in this book.

Example Project
Now that you have your environment ready, let’s start writing our first Angular2 application!
Open up the code download that came with this book and unzip it. In your terminal, cd into the
first_app/angular2-reddit-base directory:

1 $ cd first_app/angular2-reddit-base

⁸https://nodejs.org/download/
⁹https://www.cygwin.com/
Writing your First Angular 2 Web Application 5

If you’re not familiar with cd, it stands for “change directory”. If you’re on a Mac try the
following:

1. Open up /Applications/Utilities/Terminal.app
2. Type cd, without hitting enter
3. In the Finder, Drag the first_app/angular2-reddit-base folder on to your termi-
nal window
4. Hit Enter Now you are cded into the proper directory and you can move on to the
next step!

Let’s first use npm to install all the dependencies:

1 $ npm install

Create a new index.html file in the root of the project and add some basic structure:

1 <!doctype html>
2 <html>
3 <head>
4 <title>Angular 2 - Simple Reddit</title>
5 </head>
6 <body>
7 </body>
8 </html>

Your angular2-reddit-base directory should look something like this:

1 .
2 |-- README.md // A helpful readme
3 |-- index.html // Your index file
4 |-- index-full.html // Sample index file
5 |-- node_modules/ // installed dependencies
6 |-- package.json // npm configuration
7 |-- resources/ // images etc.
8 |-- styles.css // stylesheet
9 |-- tsconfig.json // compiler configuration
10 `-- tslint.json // code-style guidelines

Angular 2 itself is a JavaScript file. So we need to add a script tag to our index.html document
to include it. But Angular has some dependencies itself:
Writing your First Angular 2 Web Application 6

Angular’s Dependencies

You don’t strictly need to understand these dependencies in-depth in order to use Angular
2, but you do need to include them. Feel free to skip this section if you’re not that interested
in the dependencies, but make sure you copy and paste these script tags.

To run Angular 2, we depend on these four libraries:

• core-js
• zone.js
• reflect-metadata
• SystemJS

To include them, add the following inside your <head>

1 <script src="node_modules/core-js/client/shim.min.js"></script>
2 <script src="node_modules/zone.js/dist/zone.js"></script>
3 <script src="node_modules/reflect-metadata/Reflect.js"></script>
4 <script src="node_modules/systemjs/dist/system.src.js"></script>

Notice that we’re loading these .js files directly from a directory called node_-
modules. The node_modules directory will be created when you run npm install. If
you don’t have a node_modules directory, make sure your shell was “in” the directory
angular2-reddit-base (e.g. by using cd angular2-reddit-base) when you typed npm
install.

CoreJS ES6 Shim

ES6 provides shims so that legacy JavaScript engines behave as closely as possible to ECMAScript 6.
This shim isn’t strictly needed for newer versions of Safari, Chrome, etc. but it is required for older
versions of IE.

What’s a shim? Perhaps you’ve heard about shims or polyfills and you’re not sure what
they are. A shim is code that helps adapt between cross browsers to a standardized behavior.
For instance, check out this ES6 Compatibility Table¹⁰. Not every browser is completely
compatible with every feature. By using different shims we’re able to get standardized
behavior across different browsers (and environments).
See also: What is the difference between a shim and a polyfill?¹¹
¹⁰https://kangax.github.io/compat-table/es6/
¹¹http://www.2ality.com/2011/12/shim-vs-polyfill.html
Writing your First Angular 2 Web Application 7

For more information on core-js checkout the project page¹².

Zones

Zone.js¹³ is an advanced topic that we don’t need to worry about much here. For know, just know
that it is a library used by Angular, primarily for detecting changes to data. (If you’re coming from
Angular 1, you can think of zones as an automatic version of $digest. If you’re not coming from
Angular 1, you can ignore it for now.)

Reflect Metadata

Angular itself was written in Typescript, and Typescript provides annotations for adding metadata
to code. Roughly speaking, the reflect-metadata package is a polyfill that lets us use this metadata.

SystemJS

SystemJS is a module loader. That is, it helps us create modules and resolve dependencies. Module
loading in browser-side javascript is surprisingly complicated and SystemJS makes the process much
easier.

All Dependencies Loaded


Now that we’ve added all of the dependencies, here’s how our index.html should look now:

1 <!doctype html>
2 <html>
3 <head>
4 <title>Angular 2 - Simple Reddit</title>
5 <!-- Libraries -->
6 <script src="node_modules/core-js/client/shim.min.js"></script>
7 <script src="node_modules/zone.js/dist/zone.js"></script>
8 <script src="node_modules/reflect-metadata/Reflect.js"></script>
9 <script src="node_modules/systemjs/dist/system.src.js"></script>
10
11 </head>
12 <body>
13 </body>
14 </html>

Adding CSS
We also want to add some CSS styling so that our app isn’t completely unstyled. Let’s include two
stylesheets as well:
¹²https://github.com/zloirock/core-js
¹³https://github.com/angular/zone.js/
Writing your First Angular 2 Web Application 8

1 <!doctype html>
2 <html>
3 <head>
4 <title>Angular 2 - Simple Reddit</title>
5 <!-- Libraries -->
6 <script src="node_modules/core-js/client/shim.min.js"></script>
7 <script src="node_modules/zone.js/dist/zone.js"></script>
8 <script src="node_modules/reflect-metadata/Reflect.js"></script>
9 <script src="node_modules/systemjs/dist/system.src.js"></script>
10
11 <!-- Stylesheet -->
12 <link rel="stylesheet" type="text/css"
13 href="resources/vendor/semantic.min.css">
14 <link rel="stylesheet" type="text/css" href="styles.css">
15 </head>
16 <body>
17 </body>
18 </html>

For this project we’re going to be using Semantic-UI¹⁴ to help with the styling. Semantic-UI
is a CSS framework, similar to Foundation or Twitter Bootstrap. We’ve included it in the
sample code download so all you need to do is add the link tag.

Our First TypeScript


Let’s now create our first TypeScript file. Create a new file called app.ts in the same folder and add
the following code:

Notice that we suffix our TypeScript file with .ts instead of .js The problem is our browser
doesn’t know how to interpret TypeScript files. To solve this, we’ll compile our .ts to a
.js file in just a few minutes.

¹⁴http://semantic-ui.com/
Writing your First Angular 2 Web Application 9

code/first_app/angular2-reddit-base/app.ts

1 /**
2 * A basic hello-world Angular 2 app
3 */
4 import {
5 NgModule,
6 Component
7 } from '@angular/core';
8 import { BrowserModule } from '@angular/platform-browser';
9 import { platformBrowserDynamic } from "@angular/platform-browser-dynamic";
10
11 @Component({
12 selector: 'hello-world',
13 template: `
14 <div>
15 Hello world
16 </div>
17 `
18 })
19 class HelloWorld {
20 }
21
22 @NgModule({
23 declarations: [ HelloWorld ],
24 imports: [ BrowserModule ],
25 bootstrap: [ HelloWorld ],
26 })
27 class HelloWorldAppModule {}
28
29 platformBrowserDynamic().bootstrapModule(HelloWorldAppModule);

This snippet may seem scary at first, but don’t worry. We’re going to walk through it step by step.
The import statements define the modules we want to use to write our code. Here we’re importing
four things: NgModule, Component, BrowserModule, and platformBrowserDynamic. We’ll be going
over each of these things in depth, so don’t worry if you aren’t sure what they do yet.
We import NgModule, Component from the module "@angular/core". The "@angular/core" portion
tells our program where to find the dependencies that we’re looking for.
Similarly, we import BrowserModule from the module "@angular/platform-browser" and plat-
formBrowserDynamic from the module "@angular/platform-browser-dynamic". We’ll talk about
these in a minute.
Writing your First Angular 2 Web Application 10

Notice that the structure of this import is of the format import { things } from wherever. In the
{ things } part what we are doing is called destructuring. Destructuring is a feature provided ES6
and we talk more about it in the next chapter.
The idea with the import is a lot like import in Java or require in Ruby: we’re pulling in these
dependencies from another module and making these dependencies available for use in this file.

Making a Component
One of the big ideas behind Angular 2 is the idea of components.
In our Angular apps we write HTML markup that becomes our interactive application. But the
browser only knows so many tags: the built-ins like <select> or <form> or <video> all have
functionality defined by our browser creator. But what if we want to teach the browser new tags?
What if we wanted to have a <weather> tag that shows the weather? Or what if we wanted to have
a <login> tag that creates a login panel?
That is the idea behind components: we teach the browser new tags that have new functionality.

If you have a background in Angular 1, Components are the new version of directives.

Let’s create our very first component. When we have this component written, we will be able to use
it in our HTML document like so:

1 <hello-world></hello-world>

So how do we actually define a new Component? A basic Component has two parts:

1. A Component annotation
2. A component definition class

Let’s take these one at a time.


If you’ve been programming in JavaScript for a while then this next statement might seem a little
weird:

1 @Component({
2 // ...
3 })
Writing your First Angular 2 Web Application 11

What is going on here? Well if you have a Java background it may look familiar to you: they are
annotations.
Think of annotations as metadata added to your code. When we use @Component on the HelloWorld
class, we are “decorating” the HelloWorld as a Component.
We want to be able to use this component in our markup by using a <hello-world> tag. To do that
we configure the @Component and specify the selector as hello-world.

1 @Component({
2 selector: 'hello-world'
3 })

If you’re familiar with CSS selectors, XPath, or JQuery selectors you’ll know that there are lots of
ways to configure a selector. Angular adds its own special sauce to the selector mix, and we’ll cover
that later on. For now, just know that in this case we’re defining a new tag.
The selector property here indicates which DOM element this component is going to use. This way
if we have any <hello-world></hello-world> tag within a template, it will be compiled using this
Component class.

Adding a template
We can add a template to our @Component by passing the template option:

1 @Component({
2 selector: 'hello-world',
3 template: `
4 <div>
5 Hello world
6 </div>
7 `
8 })

Notice that we’re defining our template string between backticks (` … `). This is a new (and
fantastic) feature of ES6 that allows us to do multiline strings. Using backticks for multiline strings
makes it easy to put templates inside your code files.
Writing your First Angular 2 Web Application 12

Should I really be putting templates in my code files? The answer is: it depends. For
a long time the commonly held belief was that you should keep your code and templates
separate. While this might be easier for some teams, for some projects it adds overhead
because you have switch between a lot of files.
Personally, if my templates are shorter than a page I much prefer to have the templates
alongside the code (that is, within the .ts file). When I can see both the logic and the view
together and it’s easy to understand how they interact with one another.
The biggest drawback to putting your views inlined with your code is that many editors
don’t support syntax highlighting of the internal strings (yet). Hopefully we’ll see more
editors supporting syntax highlighting HTML within template strings soon.
If you want to separate your template into a different file you can use the templateUrl key
to specify the URL of a template that will be loaded. More on this later.

Booting Our Application


Now that we have our hello-world component we need to boot our Angular app. One of the great
things about Angular is that it was built to work well with large apps that running in many different
environments. However, that means that we have to setup some configuration to describe the type
of app we want to boot.
For instance, we want to run this Angular app in our browser (vs., say, on a native module device
or on a server), and so we’re going to tell Angular that we need to load the browser dependencies.
Angular also has a powerful concept of modules. When you boot an Angular app, you’re not booting
a component directly, but instead you create an NgModule which points to the component you want
to load.
We’ll explain the details below, but let’s take another look at the code:

code/first_app/angular2-reddit-base/app.ts

22 @NgModule({
23 declarations: [ HelloWorld ],
24 imports: [ BrowserModule ],
25 bootstrap: [ HelloWorld ],
26 })
27 class HelloWorldAppModule {}
28
29 platformBrowserDynamic().bootstrapModule(HelloWorldAppModule);

The first thing we see is an @NgModule annotation. Like all annotations, this @NgModule( ... ) code
adds metadata to the class immediately following (HelloWorldAppModule).
Writing your First Angular 2 Web Application 13

Our @NgModule annotation has three keys: declarations, imports, and bootstrap.
declarations defines the components in this module. In this case, we just have one, but you often
have several.
imports describes which dependencies this module has. We’re creating a browser app, so we want
to import the BrowserModule.
bootstrap tells Angular that when this module is used to bootstrap an app, we and to load the
HelloWorld component as the top-level component.

Now that we have our @NgModule defined, we can boot the app. The last line does this.
Remember that Angular can run on different platforms and so when we say platformBrowser-
Dynamic() we’re telling Angular that we’re using a browser and we want to compile Angular
“dynamically” (e.g. when we open the page in our browser vs. ahead-of-time compiling, which
we’ll talk more about much later).
Now that we have a platform, we call .bootstrapModule() and give it our HelloWorldAppModule
as the starting point.
Once the application is bootstrapped, the HelloWorld component will be rendered where the <hello-
world></hello-world> snippet is on the index.html file. Let’s try it out!

We talk more about NgModules in the section on NgModules

Loading our Application


To run our application, we need to do two things:

1. we need to tell our HTML document to import our app file


2. we need to use our <hello-world> component

Add the following to the body section:


Exploring the Variety of Random
Documents with Different Content
Athenian scouts without: moreover, so conspicuous was the interior
of the city to spectators without, that the temple of Athênê, and
Brasidas with its ministers around him, performing the ceremony of
sacrifice, was distinctly recognized. The fact was made known to
Kleon as he stood on the high ridge taking his survey, while at the
same time those who had gone near to the gates reported that the
feet of many horses and men were beginning to be seen under
them, as if preparing for a sally.[742] He himself went close to the
gate, and satisfied himself of this circumstance: we must recollect
that there was no defender on the walls, and no danger from
missiles. Anxious to avoid coming to any real engagement before his
reinforcements should arrive, he at once gave orders for retreat,
which he thought might be accomplished before the attack from
within could be fully organized; for he imagined that a considerable
number of troops would be marched out, and ranged in battle order,
before the attack was actually begun, not dreaming that the sally
would be instantaneous, made with a mere handful of men. Orders
having been proclaimed to wheel to the left, and retreat in column
on the left flank towards Eion, Kleon, who was himself on the top of
the hill with the right wing, waited only to see his left and centre
actually in march on the road to Eion, and then directed his right
also to wheel to the left and follow them.
The whole Athenian army were thus in full retreat, marching in a
direction nearly parallel to the Long Wall of Amphipolis, with their
right or unshielded side exposed to the enemy, when Brasidas,
looking over the southernmost gates of the Long Wall with his small
detachment ready marshalled near him, burst out into contemptuous
exclamations on the disorder of their array.[743] “These men will not
stand us; I see it by the quivering of their spears and of their heads.
Men who reel about in that way, never stand an assailing enemy.
Open the gates for me instantly, and let us sally out with
confidence.”
With that, both the gate of the Long Wall nearest to the palisade,
and the adjoining gate of the palisade itself, were suddenly thrown
open, and Brasidas with his one hundred and fifty chosen soldiers
issued out through them to attack the retreating Athenians. Running
rapidly down the straight road which joined laterally the road
towards Eion along which the Athenians were marching, he charged
their central division on the right flank:[744] their left wing had
already got beyond him on the road towards Eion. Taken completely
unprepared, conscious of their own disorderly array, and astounded
at the boldness of their enemy, the Athenians of the centre were
seized with panic, made not the least resistance, and presently fled.
Even the Athenian left, though not attacked at all, instead of halting
to lend assistance, shared the panic and fled in disorder. Having thus
disorganized this part of the army, Brasidas passed along the line to
press his attack on the Athenian right: but in this movement he was
mortally wounded and carried off the field, unobserved by his
enemies. Meanwhile Klearidas, sallying forth from the Thracian gate,
had attacked the Athenian right on the ridge opposite to him,
immediately after it began its retreat. But the soldiers on the
Athenian right had probably seen the previous movement of Brasidas
against the other division, and though astonished at the sudden
danger, had thus a moment’s warning, before they were themselves
assailed, to halt and take close rank on the hill. Klearidas here found
a considerable resistance, in spite of the desertion of Kleon; who,
more astonished than any man in his army by a catastrophe so
unlooked for, lost his presence of mind and fled at once; but was
overtaken by a Thracian peltast from Myrkinus and slain. His soldiers
on the right wing, however, repelled two or three attacks in front
from Klearidas, and maintained their ground, until at length the
Chalkidian cavalry and the peltasts from Myrkinus, having come forth
out of the gates, assailed them with missiles in flank and rear so as
to throw them into disorder. The whole Athenian army was thus put
to flight; the left hurrying to Eion, the men of the right dispersing
and seeking safety among the hilly grounds of Pangæus in their rear.
Their sufferings and loss in the flight, from the hands of the pursuing
peltasts and cavalry, were most severe: and when they at last again
mustered at Eion, not only the commander Kleon, but six hundred
Athenian hoplites, half of the force sent out, were found missing.[745]
So admirably had the attack been concerted, and so entire was
its success, that only seven men perished on the side of the victors.
But of those seven, one was the gallant Brasidas himself, who being
carried into Amphipolis, lived just long enough to learn the complete
victory of his troops and then expired. Great and bitter was the
sorrow which his death occasioned throughout Thrace, especially
among the Amphipolitans. He received, by special decree, the
distinguished honor of interment within their city, the universal habit
being to inter even the most eminent deceased persons in a suburb
without the walls. All the allies attended his funeral in arms and with
military honors: his tomb was encircled by a railing, and the space
immediately fronting it was consecrated as the great agora of the
city, which was remodelled accordingly. He was also proclaimed
œkist, or founder, of Amphipolis, and as such, received heroic
worship with annual games and sacrifices to his honor.[746] The
Athenian Agnon, the real founder and originally recognized œkist of
the city, was stripped of all his commemorative honors and
expunged from the remembrance of the people: his tomb and the
buildings connected with it, together with every visible memento of
his name, being destroyed. Full of hatred as the Amphipolitans now
were towards Athens,—and not merely of hatred, but of fear, since
the loss which they had just sustained of their saviour and protector,
—they felt repugnance to the idea of rendering farther worship to an
Athenian œkist. Nor was it convenient to keep up such a religious
link with Athens, now that they were forced to look anxiously to
Lacedæmon for assistance. Klearidas, as governor of Amphipolis,
superintended those numerous alterations in the city which this
important change required, together with the erection of the trophy,
just at the spot where Brasidas had first charged the Athenians;
while the remaining armament of Athens, having obtained the usual
truce and buried their dead, returned home without farther
operations.
There are few battles recorded in history wherein the disparity
and contrast of the two generals opposed has been so manifest,—
consummate skill and courage on the one side against ignorance and
panic on the other. On the singular ability and courage of Brasidas
there can be but one verdict of unqualified admiration: but the
criticism passed by Thucydidês on Kleon, here as elsewhere, cannot
be adopted without reserves. He tells us that Kleon undertook his
march, from Eion up to the hill in front of Amphipolis, in the same
rash and confident spirit with which he had embarked on the
enterprise against Pylus, in the blind confidence that no one would
resist him.[747] Now I have already, in a former chapter, shown
grounds for concluding that the anticipations of Kleon respecting the
capture of Sphakteria, far from being marked by any spirit of
unmeasured presumption, were sober and judicious, realized to the
letter without any unlooked-for aid from fortune. Nor are the
remarks, here made by Thucydidês on that affair, more reasonable
than the judgment on it in his former chapter; for it is not true, as
he here implies, that Kleon expected no resistance in Sphakteria: he
calculated on resistance, but knew that he had force sufficient to
overcome it. His fault even at Amphipolis, great as that fault was,
did not consist in rashness and presumption. This charge at least is
rebutted by the circumstance, that he himself wished to make no
aggressive movement until his reinforcements should arrive, and
that he was only constrained, against his own will, to abandon his
intended temporary inactivity during that interval, by the angry
murmurs of his soldiers, who reproached him with ignorance and
backwardness, the latter quality being the reverse of that with which
he is branded by Thucydidês.
When Kleon was thus driven to do something, his march up to
the top of the hill, for the purpose of reconnoitring the ground, was
not in itself unreasonable, and might have been accomplished in
perfect safety, if he had kept his army in orderly array, prepared for
contingencies. But he suffered himself to be completely out-
generalled and overreached by that simulated consciousness of
impotence and unwillingness to fight, which Brasidas took care to
present to him. Among all military stratagems, this has perhaps been
the most frequently practised with success against inexperienced
generals, who are thrown off their guard and induced to neglect
precaution, not because they are naturally more rash or
presumptuous than ordinary men, but because nothing except either
a high order of intellect, or special practice and training, will enable
a man to keep steadily present to his mind liabilities even real and
serious, when there is no discernible evidence to suggest their
approach; much more when there is positive evidence, artfully laid
out by a superior enemy, to create belief in their absence. A fault
substantially the same had been committed by Thucydidês himself
and his colleague Euklês a year and a half before, when they
suffered Brasidas to surprise the Strymonian bridge and Amphipolis:
not even taking common precautions, nor thinking it necessary to
keep the fleet at Eion. They were not men peculiarly rash and
presumptuous, but ignorant and unpractised, in a military sense;
incapable of keeping before them dangerous contingencies which
they perfectly knew, simply because there was no present evidence
of approaching explosion.
This military incompetence, which made Kleon fall into the trap
laid for him by Brasidas, also made him take wrong measures
against the danger, when he unexpectedly discovered at last that the
enemy within were preparing to attack him. His fatal error consisted
in giving instant order for retreat, under the vain hope that he could
get away before the enemy’s attack could be brought to bear.[748] An
abler officer, before he commenced the retreating march so close to
the hostile walls, would have taken care to marshal his men in
proper array, to warn and address them with the usual harangue,
and to wind up their courage to the fighting-point: for up to that
moment they had no idea of being called upon to fight; and the
courage of Grecian hoplites, taken thus unawares while hurrying to
get away in disorder visible both to themselves and their enemies,
without any of the usual preliminaries of battle, was but too apt to
prove deficient. To turn the right or unshielded flank to the enemy,
was unavoidable from the direction of the retreating movement; nor
is it reasonable to blame Kleon for this, as some historians have
done, or for causing his right wing to move too soon in following the
lead of the left, as Dr. Arnold seems to think. The grand fault seems
to have consisted in not waiting to marshal his men and prepare
them for standing fight during their retreat. Let us add, however,
and the remark, if it serves to explain Kleon’s idea of being able to
get away before he was actually assailed, counts as a double
compliment to the judgment as well as boldness of Brasidas, that no
other Lacedæmonian general of that day perhaps, not even
Demosthenês, the most enterprising general of Athens, would have
ventured upon an attack with so very small a band, relying
altogether upon the panic produced by his sudden movement.
But the absence of military knowledge and precaution is not the
worst of Kleon’s faults on this occasion. His want of courage at the
moment of conflict is yet more lamentable, and divests his end of
that personal sympathy which would otherwise have accompanied it.
A commander who has been out-generalled is under a double force
of obligation to exert and expose himself, to the uttermost, in order
to retrieve the consequences of his own mistakes. He will thus at
least preserve his own personal honor, whatever censure he may
deserve on the score of deficient knowledge and judgment.[749]
What is said about the disgraceful flight of Kleon himself, must be
applied, with hardly less severity of criticism, to the Athenian
hoplites under him. They behaved in a manner altogether unworthy
of the reputation of their city; especially the left wing, which seems
to have broken and run away without waiting to be attacked. And
when we read in Thucydidês, that the men who thus disgraced
themselves were among the best, and the best-armed hoplites in
Athens; that they came out unwillingly under Kleon; that they began
their scornful murmurs against him before he had committed any
fault, despising him for backwardness when he was yet not strong
enough to attempt anything serious, and was only manifesting a
reasonable prudence in waiting the arrival of expected
reinforcements; when we read this, we shall be led to compare the
expedition against Amphipolis with former manœuvres respecting
the attack of Sphakteria, and to discern other causes for its failure
besides the military incompetence of the commander. These hoplites
brought out with them from Athens the feelings prevalent among the
political adversaries of Kleon. The expedition was proposed and
carried by him, contrary to their wishes: they could not prevent it,
but their opposition enfeebled it from the beginning, kept within too
narrow limits the force assigned to it, and was one main reason
which frustrated its success.
Had Periklês been alive, Amphipolis might perhaps still have been
lost, since its capture was the fault of the officers employed to
defend it. But if lost, it would probably have been attacked and
recovered with the same energy as the revolted Samos had been,
with the full force and the best generals that Athens could furnish.
With such an armament under good officers, there was nothing at all
impracticable in the reconquest of the place; especially as at that
time it had no defence on three sides except the Strymon, and might
thus be approached by Athenian ships on that navigable river. The
armament of Kleon,[750] even if his reinforcements had arrived, was
hardly sufficient for the purpose. But Periklês would have been able
to concentrate upon it the whole strength of the city, without being
paralyzed by the contentions of political party: he would have seen
as clearly as Kleon, that the place could only be recovered by force,
and that its recovery was the most important object to which Athens
could devote her energies.
It was thus that the Athenians, partly from political intrigue,
partly from the incompetence of Kleon, underwent a disastrous
defeat instead of carrying Amphipolis. But the death of Brasidas
converted their defeat into a substantial victory. There remained no
Spartan either like or second to that eminent man, either as a soldier
or a conciliating politician; none who could replace him in the
confidence and affection of the allies of Athens in Thrace; none who
could prosecute those enterprising plans against Athens on her
unshielded side, which he had first shown to be practicable. The
fears of Athens, and the hopes of Sparta, in respect to the future,
disappeared alike with him. The Athenian generals, Phormio and
Demosthenês, had both of them acquired among the Akarnanians an
influence personal to themselves, apart from their post and from
their country: but the career of Brasidas, exhibited an extent of
personal ascendency and admiration, obtained as well as deserved,
such as had never before been paralleled by any military chieftain in
Greece: and Plato might well select him as the most suitable
historical counterpart to the heroic Achilles.[751] All the achievements
of Brasidas were his own individually, with nothing more than bare
encouragement, sometimes even without encouragement, from his
country. And when we recollect the strict and narrow routine in
which as a Spartan he had been educated, so fatal to the
development of everything like original thought or impulse, and so
completely estranged from all experience of party or political
discussion, we are amazed at his resource and flexibility of character,
his power of adapting himself to new circumstances and new
persons, and his felicitous dexterity in making himself the rallying-
point of opposite political parties in each of the various cities which
he acquired. The combination “of every sort of practical excellence,”
valor, intelligence, probity, and gentleness of dealing, which his
character presented, was never forgotten among the subject-allies of
Athens, and procured for other Spartan officers in subsequent years
favorable presumptions, which their conduct was seldom found to
realize.[752] At the time when Brasidas perished, in the flower of his
age, he was unquestionably the first man in Greece; and though it is
not given to us to predict what he would have become had he lived,
we may be sure that the future course of the war would have been
sensibly modified; perhaps even to the advantage of Athens, since
she might have had sufficient occupation at home to keep her from
the disastrous enterprise in Sicily.
Thucydidês seems to take pleasure in setting forth the gallant
exploits of Brasidas, from the first at Methônê to the last at
Amphipolis, not less than the dark side of Kleon; both, though in
different senses, the causes of his banishment. He never mentions
the latter except in connection with some proceeding represented as
unwise or discreditable. The barbarities which the offended majesty
of empire thought itself entitled to practise in ancient times against
dependencies revolted and reconquered, reach their maximum in the
propositions against Mitylênê and Skiônê: both of them are ascribed
to Kleon by name as their author. But when we come to the
slaughter of the Melians, equally barbarous, and worse in respect to
grounds of excuse, inasmuch as the Melians had never been
subjects of Athens, we find Thucydidês mentioning the deed without
naming the proposer.[753]
Respecting the foreign policy of Kleon, the facts already narrated
will enable the reader to form an idea of it as compared with that of
his opponents. I have shown grounds for believing that Thucydidês
has forgotten his usual impartiality in criticizing this personal enemy;
that in regard to Sphakteria, Kleon was really one main and
indispensable cause of procuring for his country the greatest
advantage which she obtained throughout the whole war; and that
in regard to his judgment as advocating the prosecution of war,
three different times must be distinguished: 1. After the first
blockade of the hoplites in Sphakteria; 2. After the capture of the
island; 3. After the expiration of the one year truce. On the earliest
of those three occasions he was wrong, for he seems to have shut
the door on all possibilities of negotiation, by his manner of dealing
with the Lacedæmonian envoys. On the second occasion, he had fair
and plausible grounds to offer on behalf of his opinion, though it
turned out unfortunate: moreover, at that time, all Athens was
warlike, and Kleon is not to be treated as the peculiar adviser of that
policy. On the third and last occasion, after the expiration of the
truce, the political counsel of Kleon was right, judicious, and truly
Periklêan, much surpassing in wisdom that of his opponents. We
shall see in the coming chapters how those opponents managed the
affairs of the state after his death; how Nikias threw away the
interests of Athens in the enforcement of the conditions of peace;
how Nikias and Alkibiadês together shipwrecked the power of their
country on the shores of Syracuse. And when we judge the
demagogue Kleon in this comparison, we shall find ground for
remarking that Thucydidês is reserved and even indulgent towards
the errors and vices of other statesmen, harsh only towards those of
his accuser.
As to the internal policy of Kleon, and his conduct as a politician
in Athenian constitutional life, we have but little trustworthy
evidence. There exists, indeed, a portrait of him, drawn in colors
broad and glaring, most impressive to the imagination, and hardly
effaceable from the memory; the portrait in the “Knights” of
Aristophanês. It is through this representation that Kleon has been
transmitted to posterity, crucified by a poet who admits himself to
have had a personal grudge against him, just as he has been
commemorated in the prose of an historian whose banishment he
had proposed. Of all the productions of Aristophanês, so replete with
comic genius throughout, the “Knights” is the most consummate and
irresistible; the most distinct in its character, symmetry, and purpose.
Looked at with a view to the object of its author, both in reference to
the audience and to Kleon, it deserves the greatest possible
admiration, and we are not surprised to learn that it obtained the
first prize. It displays the maximum of that which wit combined with
malice can achieve, in covering an enemy with ridicule, contempt,
and odium. Dean Swift would have desired nothing worse, even for
Ditton and Winston. The old man, Demos of Pnyx, introduced on the
stage as personifying the Athenian people,—Kleon, brought on as his
newly-bought Paphlagonian slave, who by coaxing, lying, impudent
and false denunciation of others, has gained his master’s ear, and
heaps ill-usage upon every one else, while he enriches himself,—the
Knights, or chief members of what we may call the Athenian
aristocracy, forming the Chorus of the piece as Kleon’s pronounced
enemies,—the sausage-seller from the market-place, who, instigated
by Nikias find Demosthenês along with these Knights, overdoes
Kleon in all his own low arts, and supplants him in the favor of
Demos; all this, exhibited with inimitable vivacity of expression,
forms the masterpiece and glory of libellous comedy. The effect
produced upon the Athenian audience when this piece was
represented at the Lenæan festival, January B.C. 424, about six
months after the capture of Sphakteria, with Kleon himself and most
of the real Knights present, must have been intense beyond what we
can now easily imagine. That Kleon could maintain himself after this
humiliating exposure, is no small proof of his mental vigor and
ability. It does not seem to have impaired his influence, at least not
permanently; for not only do we see him the most effective
opponent of peace during the next two years, but there is ground for
believing that the poet himself found it convenient to soften his tone
towards this powerful enemy.
So ready are most writers to find Kleon guilty, that they are
satisfied with Aristophanês as a witness against him: though no
other public man, of any age or nation, has ever been condemned
upon such evidence. No man thinks of judging Sir Robert Walpole, or
Mr. Fox, or Mirabeau, from the numerous lampoons put in circulation
against them: no man will take measure of a political Englishman
from Punch, or of a Frenchman from the Charivari. The unrivalled
comic merit of the “Knights” of Aristophanês is only one reason the
more for distrusting the resemblance of its picture to the real Kleon.
We have means too of testing the candor and accuracy of
Aristophanês by his delineation of Sokratês, whom he introduced in
the comedy of “Clouds” in the year after that of the “Knights.” As a
comedy, the “Clouds” stands second only to the “Knights”: as a
picture of Sokratês, it is little better than pure fancy: it is not even a
caricature, but a totally different person. We may indeed perceive
single features of resemblance; the bare feet, and the argumentative
subtlety, belong to both; but the entire portrait is such, that if it bore
a different name, no one would think of comparing it with Sokratês,
whom we know well from other sources. With such an analogy
before us, not to mention what we know generally of the portraits of
Periklês by these authors, we are not warranted in treating the
portrait of Kleon as a likeness, except on points where there is
corroborative evidence. And we may add, that some of the hits
against him, where we can accidentally test their pertinence, are
decidedly not founded in fact; as, for example, where the poet
accuses Kleon of having deliberately and cunningly robbed
Demosthenês of his laurels in the enterprise against Sphakteria.[754]
In the prose of Thucydidês, we find Kleon described as a
dishonest politician, a wrongful accuser of others, the most violent of
all the citizens:[755] throughout the verse of Aristophanês, these
same charges are set forth with his characteristic emphasis, but
others are also superadded; Kleon practises the basest artifices and
deceptions to gain favor with the people, steals the public money,
receives bribes, and extorts compositions from private persons by
wholesale, and thus enriches himself under pretence of zeal for the
public treasury. In the comedy of the Acharnians, represented one
year earlier than the Knights, the poet alludes with great delight to a
sum of five talents, which Kleon had been compelled “to disgorge”: a
present tendered to him by the insular subjects of Athens, if we may
believe Theopompus, for the purpose of procuring a remission of
their tribute, and which the Knights, whose evasions of military
service he had exposed, compelled him to relinquish.[756]
But when we put together the different heads of indictment
accumulated by Aristophanês, it will be found that they are not
easily reconcilable one with the other; for an Athenian, whose
temper led him to violent crimination of others, at the inevitable
price of multiplying and exasperating personal enemies, would find it
peculiarly dangerous, if not impossible, to carry on peculation for his
own account. If, on the other hand, he took the latter turn, he would
be inclined to purchase connivance from others even by winking at
real guilt on their part, far from making himself conspicuous as a
calumniator of innocence. We must therefore discuss the side of the
indictment which is indicated in Thucydidês; not Kleon, as truckling
to the people and cheating for his own pecuniary profit (which is
certainly not the character implied in his speech about the
Mitylenæans, as given to us by the historian),[757] but Kleon as a
man of violent temper and fierce political antipathies, a bitter
speaker, and sometimes dishonest in his calumnies against
adversaries. These are the qualities which, in all countries of free
debate, go to form what is called a great opposition speaker. It was
thus that the elder Cato, “the universal biter, whom Persephonê was
afraid even to admit into Hades after his death,” was characterized
at Rome, even by the admission of his admirers to some extent, and
in a still stronger manner by those who were unfriendly to him, as
Thucydidês was to Kleon.[758] In Cato, such a temper was not
inconsistent with a high sense of public duty. And Plutarch recounts
an anecdote respecting Kleon, that, on first beginning his political
career, he called his friends together, and dissolved his intimacy with
them, conceiving that private friendships would distract him from his
paramount duty to the commonwealth.[759]
Moreover, the reputation of Kleon as a frequent and unmeasured
accuser of others, may be explained partly by a passage of his
enemy Aristophanês: a passage the more deserving of confidence as
a just representation of fact, since it appears in a comedy (the
“Frogs”) represented (405 B.C.) fifteen years after the death of Kleon,
and five years after that of Hyperbolus, when the poet had less
motive for misrepresentations against either. In the “Frogs,” the
scene is laid in Hades, whither the god Dionysus goes, in the attire
of Hêraklês and along with his slave Xanthias, for the purpose of
bringing up again to earth the deceased poet Euripidês. Among the
incidents, Xanthias, in the attire which his master had worn, is
represented as acting with violence and insult towards two hostesses
of eating-houses; consuming their substance, robbing them, refusing
to pay when called upon, and even threatening their lives with a
drawn sword. Upon which the women, having no other redress left,
announce their resolution of calling, the one upon her protector
Kleon, the other on Hyperbolus, for the purpose of bringing the
offender to justice before the dikastery.[760] This passage shows us,
if inferences on comic evidence are to be held as admissible, that
Kleon and Hyperbolus became involved in accusations partly by
helping poor persons who had been wronged to obtain justice before
the dikastery. A rich man who had suffered injury might apply to
Antipho or some other rhetor for paid advice and aid as to the
conduct of his complaint; but a poor man or woman would think
themselves happy to obtain the gratuitous suggestion, and
sometimes the auxiliary speech, of Kleon or Hyperbolus; who would
thus extend their own popularity, by means very similar to those
practised by the leading men in Rome.[761]
But besides lending aid to others, doubtless Kleon was often also
a prosecutor, in his own name, of official delinquents, real or alleged.
That some one should undertake this duty was indispensable for the
protection of the city; otherwise, the responsibility to which official
persons were subjected after their term of office would have been
merely nominal: and we have proof enough that the general public
morality of these official persons, acting individually, was by no
means high. But the duty was at the same time one which most
persons would and did shun. The prosecutor, while obnoxious to
general dislike, gained nothing even by the most complete success;
and if he failed so much as not to procure a minority of votes among
the dikasts, equal to one-fifth of the numbers present, he was
condemned to pay a fine of one thousand drachms. What was still
more serious, he drew upon himself a formidable mass of private
hatred, from the friends, partisans, and the political club, of the
accused party, extremely menacing to his own future security and
comfort, in a community like Athens. There was therefore little
motive to accept, and great motive to decline, the task of
prosecuting on public grounds. A prudent politician at Athens would
undertake it occasionally, and against special rivals, but he would
carefully guard himself against the reputation of doing it frequently
or by inclination, and the orators constantly do so guard themselves
in those speeches which yet remain.
It is this reputation which Thucydidês fastens upon Kleon, and
which, like Cato the censor at Rome, he probably merited; from
native acrimony of temper, from a powerful talent for invective and
from his position, both inferior and hostile to the Athenian knights,
or aristocracy, who overshadowed him by their family importance.
But in what proportion of cases his accusations were just or
calumnious, the real question upon which a candid judgment turns,
we have no means of deciding, either in his case or that of Cato. “To
lash the wicked (observes Aristophanês himself[762]) is not only no
blame, but is even a matter of honor to the good.” It has not been
common to allow to Kleon the benefit of this observation, though he
is much more entitled to it than Aristophanês. For the attacks of a
poetical libeller admit neither of defence nor retaliation; whereas a
prosecutor before the dikastery found his opponent prepared to
reply or even to retort, and was obliged to specify his charge, as well
as to furnish proof of it; so that there was a fair chance for the
innocent man not to be confounded with the guilty.
The quarrel of Kleon with Aristophanês is said to have arisen out
of an accusation which he brought against that poet[763] in the
Senate of Five Hundred, on the subject of his second comedy, the
“Babylonians,” exhibited B.C. 426, at the festival of the urban
Dionysia in the month of March. At that season many strangers were
present at Athens, and especially many visitors and deputies from
the subject-allies, who were bringing their annual tribute: and as the
“Babylonians,” (now lost), like so many other productions of
Aristophanês, was full of slashing ridicule, not only against individual
citizens but against the functionaries and institutions of the city,[764]
Kleon instituted a complaint against it in the senate, as an exposure
dangerous to the public security before strangers and allies. We
have to recollect that Athens was then in the midst of an
embarrassing war; that the fidelity of her subject-allies was much
doubted; that Lesbos, the greatest of her allies, had been
reconquered only in the preceding year, after a revolt both
troublesome and perilous to the Athenians. Under such
circumstances, Kleon had good reason for thinking that a political
comedy of the Aristophanic vein and talent tended to degrade the
city in the eyes of strangers, even granting that it was innocuous
when confined to the citizens themselves. The poet complains[765]
that Kleon summoned him before the senate, with terrible threats
and calumny: but it does not appear that any penalty was inflicted.
Nor, indeed, had the senate competence to find him guilty or punish
him except to the extent of a small fine: they could only bring him to
trial before the dikastery, which in this case plainly was not done. He
himself, however, seems to have felt the justice of the warning: for
we find that three out of his four next following plays, before the
Peace of Nikias,—the Acharnians, the Knights, and the Wasps,—were
represented at the Lenæan festival,[766] in the month of January, a
season when no strangers nor allies were present. Kleon was
doubtless much incensed with the play of the Knights, and seems to
have annoyed the poet either by bringing an indictment against him
for exercising freemen’s rights without being duly qualified, since
none but citizens were allowed to appear and act in the dramatic
exhibitions, or by some other means which are not clearly explained.
Nor can we make out in what way the poet met him, though it
appears that finding less public sympathy than he thought himself
entitled to, he made an apology without intending to be bound by it.
[767] Certain it is, that his remaining plays subsequent to the Knights,
though containing some few bitter jests against Kleon, manifest no
second deliberate set against him.
The battle of Amphipolis removed at once the two most
pronounced individual opponents of peace, Kleon and Brasidas.
Athens too was more than ever discouraged and averse to prolonged
fighting; for the number of hoplites slain at Amphipolis doubtless
filled the city with mourning, besides the unparalleled disgrace now
tarnishing Athenian soldiership. The peace-party under the auspices
of Nikias and Lachês, relieved at once from the internal opposition of
Kleon, as well as from the foreign enterprise of Brasidas, were
enabled to resume their negotiations with Sparta in a spirit
promising success. King Pleistoanax, and the Spartan ephors of the
year, were on their side equally bent on terminating the war, and the
deputies of all the allies were convoked at Sparta for discussion with
the envoys of Athens. Such discussion was continued during the
whole autumn and winter after the battle of Amphipolis, without any
actual hostilities on either side. At first, the pretensions advanced
were found very conflicting; but at length, after several debates, it
was agreed to treat upon the basis of each party surrendering what
had been acquired by war. The Athenians insisted at first on the
restoration of Platæa; but the Thebans replied that Platæa was
theirs neither by force nor by treason, but by voluntary capitulation
and surrender of the inhabitants. This distinction seems to our ideas
somewhat remarkable, since the capitulation of a besieged town is
not less the result of force than capture by storm. But it was
adopted in the present treaty; and under it the Athenians, while
foregoing their demand of Platæa, were enabled to retain Nisæa,
which they had acquired from the Megarians, and Anaktorium and
Sollium,[768] which they had taken from Corinth. To insure
accommodating temper on the part of Athens, the Spartans held out
the threat of invading Attica in the spring, and of establishing a
permanent fortification in the territory: and they even sent round
proclamation to their allies, enjoining all the details requisite for this
step. Since Attica had now been exempt from invasion for three
years, the Athenians were probably not insensible to this threat of
renewal under a permanent form.
At the beginning of spring, about the end of March, 421 B.C.,
shortly after the urban Dionysia at Athens, the important treaty was
concluded for the term of fifty years. The following were its principal
conditions:—
1. All shall have full liberty to visit all the public temples of
Greece, for purposes of private sacrifice, consultation of oracle, or
public sacred mission. Every man shall be undisturbed both in going
and coming. [The value of this article will be felt, when we recollect
that the Athenians and their allies had been unable to visit the
Olympic or Pythian festival since the beginning of the war.]
2. The Delphians shall enjoy full autonomy and mastery of their
temple and their territory. [This article was intended to exclude the
ancient claim of the Phocian confederacy to the management of the
temple; a claim which the Athenians had once supported, before the
thirty years’ truce: but they had now little interest in the matter,
since the Phocians were in the ranks of their enemies.]
3. There shall be peace for fifty years, between Athens and
Sparta with their respective allies, with abstinence from mischief,
either overt or fraudulent, by land as well as by sea.
4. Neither party shall invade for purposes of mischief the territory
of the other, not by any artifice or under any pretence.
Should any subject of difference arise, it shall be settled by
equitable means, and by oaths tendered and taken, in form to be
hereafter agreed on.
5. The Lacedæmonians and their allies shall restore Amphipolis to
the Athenians.
They shall farther relinquish to the Athenians Argilus, Stageirus,
Akanthus, Skôlus, Olynthus, and Spartôlus. But these cities shall
remain autonomous, on condition of paying tribute to Athens
according to the assessment of Aristeidês. Any of their citizens who
may choose to quit them shall be at liberty to do so, and to carry
away his property. Nor shall the cities be counted hereafter either as
allies of Athens or of Sparta, unless Athens shall induce them by
amicable persuasions to become her allies, which she is at liberty to
do if she can.
The inhabitants of Mekyberna, Sanê, and Singê, shall dwell
independently in their respective cities, just as much as the
Olynthians and Akanthians. [These were towns which adhered to
Athens, and were still numbered as her allies; though they were
near enough to be molested by Olynthus[769] and Akanthus, against
which this clause was intended to insure them.]
The Lacedæmonians and their allies shall also restore Panaktum
to the Athenians.
6. The Athenians shall restore to Sparta Koryphasium, Kythêra,
Methônê, Pteleum, Atalantê, with all the captives in their hands from
Sparta or her allies. They shall farther release all Spartans or allies of
Sparta now blocked up in Skiônê.
7. The Lacedæmonians and their allies shall also restore all the
captives in their hands, from Athens or her allies.
8. Respecting Skiônê, Torônê, Sermylus, or any other town in the
possession of Athens, the Athenians may take their own measures.
9. Oaths shall be exchanged between the contracting parties,
according to the solemnities held most binding in each city
respectively, and in the following words: “I will adhere to this
convention and truce sincerely and without fraud.” The oaths shall
be annually renewed, and the terms of peace shall be inscribed on
columns at Olympia, Delphi, and the Isthmus, as well as at Sparta
and Athens.
10. Should any matter have been forgotten in the present
convention, the Athenians and Lacedæmonians may alter it by
mutual understanding and consent, without being held to violate
their oaths.
These oaths were accordingly exchanged: they were taken by
seventeen principal Athenians, and as many Spartans, on behalf of
their respective countries, on the 26th day of the month Artemisius
at Sparta, and on the 24th day of Elaphebolion at Athens,
immediately after the urban Dionysia; Pleistolas being ephor
eponymus at Sparta, and Alkæus archon eponymus at Athens.
Among the Lacedæmonians swearing, are included the two kings
Agis and Pleistoanax, the ephor Pleistolas, and perhaps other
ephors, but this we do not know, and Tellis, the father of Brasidas.
Among the Athenians sworn, are comprised Nikias, Lachês, Agnon,
Lamachus, and Demosthenês.[770]
Such was the peace—commonly known by the name of the
Peace of Nikias—concluded in the beginning of the eleventh spring
of the war, which had just lasted ten full years. Its conditions were
put to the vote at Sparta, in the assembly of deputies from the
Lacedæmonian allies, the majority of whom accepted them: which,
according to the condition adopted and sworn to by every member
of the confederacy,[771] made it binding upon all. There was, indeed,
a special reserve allowed to any particular state in case of religious
scruple, arising out of the fear of offending some of their gods or
heroes, but, saving this reserve, the peace had been formally
acceded to by the decision of the confederates. But it soon appeared
how little the vote of the majority was worth, even when enforced
by the strong pressure of Lacedæmon herself, when the more
powerful members were among the dissentient minority. The
Bœotians, Megarians, and Corinthians, all refused to accept it; nor
does it seem that any deputies from the allies took the oath along
with the Lacedæmonian envoys; though the truce for a year, two
years before,[772] had been sworn to by Lacedæmonian, Corinthian,
Megarian, Sikyonian, and Epidaurian envoys.
The Corinthians were displeased because they did not recover
Sollium and Anaktorium; the Megarians, because they did not regain
Nisæa; the Bœotians, because they were required to surrender
Panaktum. In spite of the urgent solicitations of Sparta, the deputies
of all these powerful states not only denounced the peace as unjust,
and voted against it in the general assembly of allies, but refused to
accept it when the vote was carried, and went home to their
respective cities for instructions.[773]
Such were the conditions, and such the accompanying
circumstances, of the Peace of Nikias, which terminated, or
professed to terminate, the great Peloponnesian war, after a duration
of ten years. Its consequences and fruits, in many respects such as
were not anticipated by either of the concluding parties, will be seen
in my next volume.
FOOTNOTES
[1] Xenophon, Memorab. iii, 5, 18.

[2] Thucyd. v. 30: about the Spartan confederacy,—εἰρημένον, κύριον εἶναι,


ὅ,τι ἂν τὸ πλῆθος τῶν ξυμμάχων ψηφίσηται, ἢν μή τι θεῶν ἢ ἡρώων κώλυμα ᾖ.

[3] Thucyd. ii, 63. τῆς τε πόλεως ὑμᾶς εἰκὸς τῷ τιμωμένῳ ἀπὸ τοῦ ἄρχειν,
ᾧπερ ἅπαντες ἀγάλλεσθε, βοηθεῖν, καὶ μὴ φεύγειν τοὺς πόνους, ἢ μηδὲ τὰς τιμὰς
διώκειν, etc.

[4] Plutarch, Periklês, c. 12.

[5] Plutarch, Periklês, c. 11.

[6] Aristophan. Vesp. 707.

[7] The island of Kythêra was conquered by the Athenians from Sparta in 425
B.C., and the annual tribute then imposed upon it was four talents (Thucyd. iv,
57). In the Inscription No. 143, ap. Boeckh, Corp. Inscr., we find some names
enumerated of tributary towns, with the amount of tribute opposite to each, but
the stone is too much damaged to give us much information. Tyrodiza, in Thrace,
paid one thousand drachms: some other towns, or junctions of towns, not clearly
discernible, are rated at one thousand, two thousand, three thousand drachms,
one talent, and even ten talents. This inscription must be anterior to 415 B.C.,
when the tribute was converted into a five per cent. duty upon imports and
exports: see Boeckh, Public Econ. of Athens, and his Notes upon the above-
mentioned Inscription.
It was the practice of Athens not always to rate each tributary city separately,
but sometimes to join several in one collective rating; probably each responsible
for the rest. This seems to have provoked occasional remonstrances from the
allies, in some of which the rhetor, Antipho, was employed to furnish the speech
which the complainants pronounced before the dikastery: see Antipho ap.
Harpokration, v. Ἀπόταξις—Συντελεῖς. It is greatly to be lamented that the
orations composed by Antipho, for the Samothrakians and Lindians,—the latter
inhabiting one of the three separate towns in the island of Rhodes,—have not
been preserved.

[8] Xenophon, Anab. vii, 1, 27. οὐ μεῖον χιλίων ταλάντων: compare Boeckh,
Public Econ. of Athens, b. iii, ch. 7, 15, 19.

[9] Aristophan. Vesp. 660. τάλαντ᾽ ἐγγὺς δισχίλια.

[10] Very excellent writers on Athenian antiquity (Boeckh, Public Econ. of


Athens, c. 15, 19, b. iii; Schömann, Antiq. J. P. Att. sect. lxxiv; K. F. Hermann, Gr.
Staatsalterthümer, sect. 157: compare, however, a passage in Boeckh, ch. 17, p.
421, Eng. transl., where he seems to be of an opposite opinion) accept this
statement, that the tribute levied by Athenians upon her allies was doubled some
years after the commencement of the Peloponnesian war,—at which time it was
six hundred talents,—and that it came to amount to twelve hundred talents.
Nevertheless, I cannot follow them, upon the simple authority of Æschinês, and
the Pseudo-Andokidês (Æschin. De Fals. Legat. c. 54, p. 301; Andokidês, De
Pace, c. 1, and the same orator cont. Alkibiad. c. 4). For we may state pretty
confidently, that neither of the two orations here ascribed to Andokidês is
genuine: the oration against Alkibiadês most decidedly not genuine. There
remains, therefore, as an original evidence, only the passage of Æschinês, which
has, apparently, been copied by the author of the Oration De Pace, ascribed to
Andokidês. Now the chapter of Æschinês, which professes to furnish a general
but brief sketch of Athenian history for the century succeeding the Persian
invasion, is so full of historical and chronological inaccuracies, that we can hardly
accept it, when standing alone, as authority for any matter of fact. In a note on
the chapter immediately preceding, I have already touched upon its extraordinary
looseness of statement,—pointed out by various commentators, among them
particularly by Mr. Fynes Clinton: see above, chap. xlv, note 2, pp. 409-411, in the
preceding volume.
The assertion, therefore, that the tribute from the Athenian allies was raised
to the sum of twelve hundred talents annually, comes to us only from the orator
Æschinês as an original witness: and in him it forms part of a tissue of statements
alike confused and incorrect. But against it we have a powerful negative
argument,—the perfect silence of Thucydidês. Is it possible that that historian
would have omitted all notice of a step so very important in its effects, if Athens
had really adopted it? He mentions to us the commutation by Athens of the
tribute from her allies into a duty of five per cent. payable by them on their
exports and imports (vii, 28)—this was in the nineteenth year of the war, 413 B.C.
But anything like the duplication of the tribute all at once, would have altered
much more materially the relations between Athens and her allies and would have
constituted in the minds of the latter a substantive grievance, such as to
aggravate the motive for revolt in a manner which Thucydidês could hardly fail to
notice. The orator Æschinês refers the augmentation of the tribute, up to twelve
hundred talents, to the time succeeding the peace of Nikias: M. Boeckh (Public
Econ. of Athens, b. iii, ch. 15-19, pp. 400-434) supposes it to have taken place
earlier than the representation of the Vespæ of Aristophanês, that is, about three
years before that peace, or 423 B.C. But this would have been just before the
time of the expedition of Brasidas into Thrace, and his success in exciting revolt
among the dependencies of Athens: if Athens had doubled her tribute upon all
the allies, just before that expedition, Thucydidês could not have omitted to
mention it, as increasing the chances of success to Brasidas, and helping to
determine the resolutions of the Akanthians and others, which were by no means
adopted unanimously or without hesitation, to revolt.
In reference to the oration called that of Andokidês against Alkibiadês, I made
some remarks in the fourth volume of this History (vol. iv, ch. xxxi, p. 151),
tending to show it to be spurious and of a time considerably later than that to
which it purports to belong. I will here add one other remark, which appears to
me decisive, tending to the same conclusion.
The oration professes to be delivered in a contest of ostracism between Nikias,
Alkibiadês, and the speaker: one of the three, he says, must necessarily be
ostracized, and the question is, to determine which of the three: accordingly, the
speaker dwells upon many topics calculated to raise a bad impression of
Alkibiadês, and a favorable impression of himself.
Among the accusations against Alkibiadês, one is, that after having
recommended, in the assembly of the people, that the inhabitants of Melos
should be sold as slaves, he had himself purchased a Melian woman among the
captives, and had had a son by her: it was criminal, argues the speaker, to beget
offspring by a woman whose relations he had contributed to cause to be put to
death, and whose city he had contributed to ruin (c. 8).
Upon this argument I do not here touch, any farther than to bring out the
point of chronology. The speech, if delivered at all, must have been delivered, at
the earliest, nearly a year after the capture of Melos by the Athenians: it may be
of later date, but it cannot possibly be earlier.
Now Melos surrendered in the winter immediately preceding the great
expedition of the Athenians to Sicily in 415 B.C., which expedition sailed about
midsummer (Thucyd. v, 116; vi, 30). Nikias and Alkibiadês both went as
commanders of that expedition: the latter was recalled to Athens for trial on the
charge of impiety about three months afterwards, but escaped in the way home,
was condemned and sentenced to banishment in his absence, and did not return
to Athens until 407 B.C., long after the death of Nikias, who continued in
command of the Athenian armament in Sicily, enjoying the full esteem of his
countrymen, until its complete failure and ruin before Syracuse,—and perished
himself afterwards as a Syracusan prisoner.
Taking these circumstances together, it will at once be seen that there never
can have been any time, ten months or more after the capture of Melos, when
Nikias and Alkibiadês could have been exposed to a vote of ostracism at Athens.
The thing is absolutely impossible: and the oration in which such historical and
chronological incompatibilities are embodied, must be spurious: furthermore, it
must have been composed long after the pretended time of delivery, when the
chronological series of events had been forgotten.
I may add that the story of this duplication of the tribute by Alkibiadês is
virtually contrary to the statement of Plutarch, probably borrowed from Æschinês,
who states that the demagogues gradually increased (κατὰ μικρὸν) the tribute to
thirteen hundred talents (Plutarch, Aristeid. c. 24).

[11] Thucyd. ii, 13.

[12] Thucyd. i, 80. The foresight of the Athenian people, in abstaining from
immediate use of public money and laying it up for future wants, would be still
more conspicuously demonstrated, if the statement of Æschinês, the orator, were
true, that they got together seven thousand talents between the peace of Nikias
and the Sicilian expedition. M. Boeckh believes this statement, and says: “It is not
impossible that one thousand talents might have been laid by every year, as the
amount of tribute received was so considerable.” (Public Economy of Athens, ch.
xx. p. 446, Eng. Trans.) I do not believe the statement: but M. Boeckh and
others, who do admit it, ought in fairness to set it against the many remarks
which they pass in condemnation of the democratical prodigality.

[13] Thucyd. i. 122-143; ii, 13. The πεντηκοστὴ, or duty of two per cent.
upon imports and exports at the Peiræus, produced to the state a revenue of
thirty-six talents in the year in which it was farmed by Andokidês, somewhere
about 400 B.C., after the restoration of the democracy at Athens from its defeat
and subversion at the close of the Peloponnesian war (Andokidês de Mysteriis, c.
23, p. 65). This was at a period of depression in Athenian affairs, and when trade
was doubtless not near so good as it had been during the earlier part of the
Peloponnesian war.
It seems probable that this must have been the most considerable permanent
source of Athenian revenue next to the tribute; though we do not know what rate
of customs-duty was imposed at the Peiræus during the Peloponnesian war.
Comparing together the two passages of Xenophon (Republ. Ath. 1, 17, and
Aristophan. Vesp. 657), we may suppose that the regular and usual rate of duty
was one per cent. or one ἑκατοστὴ,—while in case of need this may have been
doubled or tripled.—τὰς πολλὰς ἑκατοστάς, (see Boeckh, b. iii, chs. 1-4, pp. 298-
318, Eng. Trans.) The amount of revenue derived even from this source, however,
can have borne no comparison to the tribute.

[14] By Periklês, Thucyd. ii, 63. By Kleon, Thucyd. iii, 37. By the envoys at
Melos, v, 89. By Euphemus, vi, 85. By the hostile Corinthians, i, 124 as a matter
of course.

[15] Plutarch, Periklês. c. 20.

[16] Plutarch, Kimon. c. 14.

[17] Plutarch, Periklês, c. 19, 20.

[18] Xenophon, Rep. Ath. ii, 16. τὴν μὲν οὐσίαν ταῖς νήσοις παρατίθενται,
πιστεύοντες τῇ ἀρχῇ τῇ κατὰ θάλασσαν· τὴν δὲ Ἀττικὴν γῆν περιορῶσι
τεμνομένην, γιγνώσκοντες ὅτι εἰ αὐτὴν ἐλεήσουσιν, ἑτέρων ἀγαθῶν μειζόνων
στερήσονται.
Compare also Xenophon (Memorabil. ii, 8, 1, and Symposion, iv, 31).

[19] See the case of the free laborer and the husbandman at Naxos, Plato,
Euthyphro, c. 3.

[20] Thucyd. i. 100.

[21] Thucyd. iv, 105; Marcellinus, Vit. Thucyd. c. 19. See Rotscher, Leben des
Thukydides, ch. i, 4, p. 96, who gives a genealogy of Thucydidês, as far as it can
be made out with any probability. The historian was connected by blood with
Miltiadês and Kimon, as well as with Olorus, king of one of the Thracian tribes,
whose daughter Hegesipylê was wife of Miltiadês, the conqueror of Marathon. In
this manner, therefore, he belonged to one of the ancient heroic families of
Athens, and even of Greece, being an Ækid through Ajax and Philæus (Marcellin.
c. 2).

[22] Thucyd. iv, 102; v, 6.

[23] Diodor. xii, 35.

[24] Diodor. xii, 11, 12; Strabo. vi, 264: Plutarch, Periklês, c. 22.

[25] The Athenians pretended to no subject allies beyond the Ionian gulf,
Thucyd. vi, 14: compare vi, 45, 104; vii, 34. Thucydidês does not even mention
Thurii, in his catalogue of the allies of Athens at the beginning of the
Peloponnesian war (Thucyd. ii, 15).

[26] Plutarch, Periklês, c. 11.

[27] Compare the speech of Nikias, in reference to the younger citizens and
partisans of Alkibiadês sitting together near the latter in the assembly,—οὓς ἐγὼ
ὁρῶν νῦν ἐνθάδε τῷ αὐτῷ ἀνδρὶ π α ρ α κ ε λ ε υ σ τ ο ὺ ς κ α θ η μ έ ν ο υ ς φοβοῦμαι,
καὶ τοῖς πρεσβυτέροις ἀντιπαρακελεύομαι μὴ καταισχυνθῆναι, εἴ τῴ τις
παρακάθηται τῶνδε, etc. (Thucyd. vi, 13.) See also Aristophanês, Ekklesiaz. 298,
seq., about partisans sitting near together.

[28] Plutarch, Periklês, c. 8. Ὅταν ἐγὼ καταβάλω παλαίων, ἐκεῖνος ἀντιλέγων


ὡς οὐ πέπτωκε, νικᾷ, καὶ μεταπείθει τοὺς ὁρῶντας.

[29] Plutarch, Periklês, c. 11. ἡ δ᾽ ἐκείνων ἅμιλλα καὶ φιλοτιμία τῶν ἀνδρῶν
βαθυτάτην τομὴν τεμοῦσα τῆς πόλεως, τὸ μὲν δῆμον, τὸ δ᾽ ὀλίγους ἐποίησε
καλεῖσθαι.

[30] Plutarch, Periklês, c. 12. διέβαλλον ἐν ταῖς ἐκκλησίαις βοῶντες, ὡς ὁ μὲν


δῆμος ἀδοξεῖ καὶ κακῶς ἀκούει τὰ κοινὰ τῶν Ἑλλήνων χρήματα πρὸς αὑτὸν ἐκ
Δήλου μεταγαγών, ἣ δ᾽ ἔνεστιν αὐτῷ πρὸς τοὺς ἐγκαλοῦντας εὐπρεπεστάτη τῶν
προφάσεων, δείσαντα τοὺς βαρβάρους ἐκεῖθεν ἀνελέσθαι καὶ φυλάττειν ἐν ὀχυρῷ
τὰ κοινά, ταύτην ἀνῄρηκε Περικλῆς, etc.
Compare the speech of the Lesbians, and their complaints against Athens, at
the moment of their revolt in the fourth year of the Peloponnesian war (Thucyd.
iii, 10); where a similar accusation is brought forward,—ἐπειδὴ δὲ ἑωρῶμεν
αὐτοὺς (the Athenians) τὴν μὲν τοῦ Μήδου ἔχθραν ἀνιέντας, τὴν δὲ τῶν
ξυμμάχων δούλωσιν ἐπαγομένους, etc.

[31] Plutarch, Periklês, c. 20.

[32] Thucyd. i, 10.

[33] Plutarch, Periklês, c. 11-14. Τέλος δὲ πρὸς τὸν Θουκυδίδην ε ἰ ς ἀ γ ῶ ν α


περὶ τοῦ ὀστράκου καταστὰς κ α ὶ δ ι α κ ι ν δ υ ν ε ύ σ α ς, ἐκεῖνον μὲν ἐξέβαλε,
κατέλυσε δὲ τὴν ἀντιτεταγμένην ἑταιρείαν. See, in reference to the principle of
the ostracism, a remarkable incident at Magnesia, between two political rivals,
Krêtinês and Hermeias: also the just reflections of Montesquieu, Esprit des Loix,
xxvi, c. 17; xxix, c. 7.

[34] Plutarch, Periklês, c. 16: the indication of time, however, is vague.

[35] Plato, Gorgias, p. 455, with Scholia; Plutarch, Periklês, c. 13:


Forchhammer, Topographie von Athen, in Kieler Philologische Studien, pp. 279-
282.

[36] Isokratês, Orat. vii: Areopagit. p. 153. c. 27.

[37] See Dikæarchus, Vit. Græciæ, Fragm. ed. Fuhr. p. 140: compare the
description of Platæa in Thucydidês, ii, 3.
All the older towns now existing in the Grecian islands are put together in this
same manner,—narrow, muddy, crooked ways,—few regular continuous lines of
houses: see Ross, Reisen in den Griechischen Inseln, Letter xxvii, vol. ii, p. 20.

[38] Aristotle, Politic. ii, 5, 1; Xenophon, Hellen. ii, 4, 1; Harpokration, v,


Ἱπποδάμεια.

[39] Diodor, xii, 9.

[40] Leake, Topography of Athens, Append. ii and iii, pp. 328-336, 2d edit.

[41] See Leake, Topography of Athens, 2d ed. p. 111, Germ. transl. O. Müller
(De Phidiæ Vitâ, p. 18) mentions no less than eight celebrated statues of Athênê,
by the hand of Pheidias,—four in the acropolis of Athens.

[42] Plutarch, Periklês, c. 13-15; O. Müller, De Phidiæ Vitâ, pp 34-60, also his
work, Archäologie der Kunst, sects. 108-113.

[43] Thucyd. i, 80. καὶ τοῖς ἄλλοις ἅπασιν ἄριστα ἐξήρτυνται, πλούτῳ τε ἰδίῳ
καὶ δημοσίῳ καὶ ναυσὶ καὶ ἵπποις καὶ ὅπλοις, καὶ ὄχλῳ ὅσος οὐκ ἐν ἄλλῳ ἑνί γε
χωρίῳ Ἑλληνικῷ ἐστὶν, etc.

[44] Plutarch, Periklês, c. 13.

[45] Thucyd. i, 10.

[46] See Leake, Topography of Athens, Append. iii, p. 329, 2d ed. Germ.
transl. Colonel Leake, with much justice, contends that the amount of two
thousand and twelve talents, stated by Harpokration out of Philochorus as the
cost of the Propylæa alone, must be greatly exaggerated. Mr. Wilkins
(Atheniensia, p. 84) expresses the same opinion; remarking that the transport of
marble from Pentelikus to Athens is easy and on a descending road.
Demetrius Phalereus (ap. Cicer. de Officiis, ii, 17) blamed Periklês for the large
sum expended upon the Propylæa; nor is it wonderful that he uttered this
censure, if he had been led to rate the cost of them at two thousand and twelve
talents.

[47] Valer. Maxim. i, 7, 2.

[48] Thucyd. ii, 13.

[49] Plutarch, Periklês, c. 17. Plutarch gives no precise date, and O. Müller
(De Phidiæ Vitâ, p. 9) places these steps for convocation of a congress before the
first war between Sparta and Athens and the battle of Tanagra,—i. e., before 460
B.C. But this date seems to me improbable: Thebes was not yet renovated in
power, nor had Bœotia as yet recovered from the fruits of her alliance with the
Persians; moreover, neither Athens nor Periklês himself seem to have been at that
time in a situation to conceive so large a project; which suits in every respect
much better for the later period, after the thirty years’ truce, but before the
Peloponnesian war.

[50] Thucyd. i, 115; viii, 76; Plutarch, Periklês, c. 28.

[51] Thucyd. i, 115; Plutarch, Periklês, c. 25. Most of the statements which
appear in this chapter of Plutarch—over and above the concise narrative of
Thucydidês—appear to be borrowed from exaggerated party stories of the day.
We need make no remark upon the story, that Periklês was induced to take the
side of Milêtus against Samos, by the fact that Aspasia was a native of Milêtus.
Nor is it at all more credible that the satrap Pissuthnês, from good-will towards
Samos, offered Periklês ten thousand golden staters as an inducement to spare
Samos. It may perhaps be true however, that the Samian oligarchy, and those
wealthy men whose children were likely to be taken as hostages, tried the effect
of large bribes upon the mind of Periklês, to prevail upon him not to alter the
government.

[52] Thucyd. i, 114, 115.

[53] Strabo, xiv, p. 638; Schol. Aristeidês, t. iii, p. 485, Dindorf.

[54] See the interesting particulars recounted respecting Sophoklês by the


Chian poet, Ion, who met and conversed with him during the course of this
expedition (Athenæus, xiii, p. 603). He represents the poet as uncommonly
pleasing and graceful in society, but noway distinguished for active capacity.
Sophoklês was at this time in peculiar favor, from the success of his tragedy,
Antigonê, the year before. See the chronology of these events discussed and
elucidated in Boeckh’s preliminary Dissertation to the Antigonê, c. 6-9.

[55] Diodor. xi, 27.

[56] Plutarch, Periklês, c. 26. Plutarch seems to have had before him
accounts respecting this Samian campaign, not only from Ephorus, Stesimbrotus,
and Duris, but also from Aristotle: and the statements of the latter must have
differed thus far from Thucydidês, that he affirmed Melissus the Samian general
to have been victorious over Periklês himself, which is not to be reconciled with
the narrative of Thucydidês.
The Samian historian, Duris, living about a century after this siege, seems to
have introduced many falsehoods respecting the cruelties of Athens: see Plutarch,
l. c.

[57] It appears very improbable that this Thucydidês can be the historian
himself. If it be Thucydidês son of Melêsias, we must suppose him to have been
restored from ostracism before the regular time,—a supposition indeed noway
inadmissible in itself, but which there is nothing else to countenance. The author
of the Life of Sophoklês, as well as most of the recent critics, adopt this opinion.
On the other hand, it may have been a third person named Thucydidês; for
the name seems to have been common, as we might guess from the two words
of which it is compounded. We find a third Thucydidês mentioned viii, 92—a
native of Pharsalus: and the biographer, Marcellinus seems to have read of many
persons so called (Θουκύδιδαι πολλοὶ, p. xvi, ed. Arnold). The subsequent history
of Thucydidês son of Melêsias, is involved in complete obscurity. We do not know
the incident to which the remarkable passage in Aristophanês (Acharn. 703)
alludes,—compare Vespæ, 946: nor can we confirm the statement which the
Scholiast cites from Idomeneus, to the effect that Thucydidês was banished and
fled to Artaxerxes: see Bergk. Reliq. Com. Att. p. 61.

[58] Thucyd. i, 117; Diodor. xii, 27, 28; Isokratês, De Permutat. Or. xv, sect.
118; Cornel. Nepos, Vit. Timoth. c. 1.
The assertion of Ephorus (see Diodorus, xii, 28, and Ephori Fragm. 117 ed.
Marx, with the note of Marx) that Periklês employed battering machines against
the town, under the management of the Klazomenian Artemon, was called in
question by Herakleidês Ponticus, on the ground that Artemon was a
contemporary of Anakreon, near a century before: and Thucydidês represents
Periklês to have captured the town altogether by blockade.

[59] Thucyd. i, 40, 41.

[60] Thucyd. viii, 21.

[61] Compare Wachsmuth, Hellenische Alterthumskunde, sect. 58, vol. ii, p.


82.

[62] See Westermann, Geschichte der Beredsamkeit in Griechenland und


Rom; Diodor. xi, 33; Dionys. Hal. A. R. v, 17.
Periklês, in the funeral oration preserved by Thucydidês (ii, 35-40), begins by
saying—Οἱ μὲν πολλοὶ τῶν ἐνθάδε εἰρηκότων ἤδη ἐπαινοῦσι τ ὸ ν π ρ ο σ θ έ ν τ α
τῷ νόμῳ τὸν λόγον τόνδε, etc.
The Scholiast, and other commentators—K. F. Weber and Westermann among
the number—make various guesses as to what celebrated man is here designated
as the introducer of the custom of a funeral harangue. The Scholiast says, Solon:
Weber fixes on Kimon: Westermann, on Aristeidês: another commentator on
Themistoklês. But we may reasonably doubt whether any one very celebrated
man is specially indicated by the words τὸν προσθέντα. To commend the
introducer of the practice, is nothing more than a phrase for commending the
practice itself.
[63] Some fragments of it seem to have been preserved, in the time of
Aristotle: see his treatise De Rhetoricâ, i, 7; iii, 10, 3.

[64] Compare the enthusiastic demonstrations which welcomed Brasidas at


Skiônê (Thucyd. iv, 121).

[65] Plutarch, Periklês, c. 28; Thucyd. ii, 34.

[66] A short fragment remaining from the comic poet Eupolis (Κόλακες, Fr.
xvi, p. 493, ed. Meineke), attests the anxiety at Athens about the Samian war,
and the great joy when the island was reconquered: compare Aristophan. Vesp.
283.

[67] Thucyd. iii, 37; ii, 63. See the conference, at the island of Melos in the
sixteenth year of the Peloponnesian war (Thucyd. v, 89, seq.), between the
Athenian commissioners and the Melians. I think, however, that this conference is
less to be trusted as based in reality, than the speeches in Thucydidês generally,
—of which more hereafter.

[68] Thucyd. iii, 47. Νῦν μὲν γὰρ ὑμῖν ὁ δῆμος ἐν ἁπάσαις ταῖς πόλεσιν
εὔνους ἐστὶ, καὶ ἢ οὐ ξυναφίσταται τοῖς ὀλίγοις, ἢ ἐὰν βιασθῇ, ὑπάρχει τοῖς
ἀποστήσασι πολέμιος εὐθὺς, etc.

[69] See the striking observations of Thucydidês, iii, 82, 83; Aristotel. Politic.
v, 6, 9.

[70] Thucyd. iii, 27.

[71] Thucyd. viii, 9-14. He observes, also, respecting the Thasian oligarchy
just set up in lieu of the previous democracy by the Athenian oligarchical
conspirators who were then organizing the revolution of the Four Hundred at
Athens,—that they immediately made preparations for revolting from Athens,—
ξυνέβη οὖν αὐτοῖς μάλιστα ἃ ἐβούλοντο, τὴν πόλιν τε ἀκινδύνως ὀρθοῦσθαι, καὶ
τ ὸ ν ἐ ν α ν τ ι ω σ ό μ ε ν ο ν δ ῆ μ ο ν κ α τ α λ ε λ ύ σ θ α ι (viii, 64).

[72] Thucyd. iv, 86, 88, 106, 123.

[73] See the important passage, Thucyd. viii, 48.

[74] Xenophon. Repub. Athen. iii, 5. πλὴν αἱ τάξεις τοῦ φόρου· τοῦτο δὲ
γίγνεται ὡς τὰ πολλὰ δι᾽ ἔτους πέμπτου.

[75] Xenophon. Repub. Athen. i, 14. Περὶ δὲ τῶν συμμάχων, οἱ ἐκπλέοντες


συκοφαντοῦσιν, ὡς δοκοῦσι, καὶ μισοῦσι τοὺς χρηστοὺς, etc.
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

textbookfull.com

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy