Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit 25ad010

Browse files
committed
docs(toh-1): code sample changes by Ward
1 parent d5d0ba8 commit 25ad010

File tree

6 files changed

+87
-80
lines changed

6 files changed

+87
-80
lines changed

public/docs/_examples/toh-1/ts-snippets/app.component.snippets.pt1.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { Component } from '@angular/core';
2+
3+
let t = {
4+
// #docregion show-hero
5+
template: `<h1>{{title}}</h1><h2>{{hero}} details!</h2>`
6+
// #enddocregion show-hero
7+
};
8+
9+
t = {
10+
// #docregion show-hero-2
11+
template: `<h1>{{title}}</h1><h2>{{hero.name}} details!</h2>`
12+
// #enddocregion show-hero-2
13+
};
14+
15+
t = {
16+
// #docregion multi-line-strings
17+
template: `
18+
<h1>{{title}}</h1>
19+
<h2>{{hero.name}} details!</h2>
20+
<div><label>id: </label>{{hero.id}}</div>
21+
<div><label>name: </label>{{hero.name}}</div>
22+
`
23+
// #enddocregion multi-line-strings
24+
};
25+
26+
27+
/*
28+
// #docregion name-input
29+
<div>
30+
<label>name: </label>
31+
<input [(ngModel)]="hero.name" placeholder="name">
32+
</div>
33+
// #enddocregion name-input
34+
*/
35+
36+
/////////////////
37+
38+
@Component(t)
39+
// #docregion app-component-1
40+
export class AppComponent {
41+
title = 'Tour of Heroes';
42+
hero = 'Windstorm';
43+
}
44+
// #enddocregion app-component-1

public/docs/_examples/toh-1/ts/app/app.component.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// #docregion pt1
1+
// #docregion
22
import { Component } from '@angular/core';
33

44
// #docregion hero-class-1
@@ -10,6 +10,7 @@ export class Hero {
1010

1111
@Component({
1212
selector: 'my-app',
13+
// #docregion editing-Hero
1314
template: `
1415
<h1>{{title}}</h1>
1516
<h2>{{hero.name}} details!</h2>
@@ -19,6 +20,7 @@ export class Hero {
1920
<input [(ngModel)]="hero.name" placeholder="name">
2021
</div>
2122
`
23+
// #enddocregion editing-Hero
2224
})
2325
export class AppComponent {
2426
title = 'Tour of Heroes';
@@ -29,4 +31,3 @@ export class AppComponent {
2931
};
3032
// #enddocregion hero-property-1
3133
}
32-
// #enddocregion pt1

public/docs/_examples/toh-1/ts/app/app.module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// #docregion
22
import { NgModule } from '@angular/core';
33
import { BrowserModule } from '@angular/platform-browser';
4-
import { FormsModule } from '@angular/forms';
4+
import { FormsModule } from '@angular/forms'; // <-- NgModel lives here
55

66
import { AppComponent } from './app.component';
77

88
@NgModule({
99
imports: [
1010
BrowserModule,
11-
FormsModule
11+
FormsModule // <-- import the FormsModule before binding with [(ngModel)]
1212
],
1313
declarations: [
1414
AppComponent

public/docs/_examples/toh-1/ts/plnkr.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"description": "Tour of Heroes: Part 1",
33
"files":[
44
"!**/*.d.ts",
5-
"!**/*.js"
5+
"!**/*.js",
6+
"!**/*.[1].*"
67
],
78
"tags": ["tutorial", "tour", "heroes"]
89
}

public/docs/ts/latest/tutorial/toh-pt1.jade

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ code-example(language="sh" class="code-shell").
4242
add two properties to the `AppComponent`: a `title` property for the app name and a `hero` property
4343
for a hero named "Windstorm."
4444

45-
+makeExample('toh-1/ts-snippets/app.component.snippets.pt1.ts', 'app-component-1', 'app.component.ts (AppComponent class)')(format=".")
45+
+makeExample('toh-1/ts/app/app.component.1.ts', 'app-component-1', 'app.component.ts (AppComponent class)')(format=".")
4646

4747
:marked
4848
Now update the template in the `@Component` decoration with data bindings to these new properties.
4949

50-
+makeExample('toh-1/ts-snippets/app.component.snippets.pt1.ts', 'show-hero')
50+
+makeExample('toh-1/ts/app/app.component.1.ts', 'show-hero', 'app.component.ts (@Component)')(format='.')
5151

5252
:marked
5353
The browser refreshes and displays the title and hero name.
@@ -79,7 +79,7 @@ code-example(language="sh" class="code-shell").
7979
Because you changed the hero from a string to an object,
8080
update the binding in the template to refer to the hero's `name` property.
8181

82-
+makeExample('toh-1/ts-snippets/app.component.snippets.pt1.ts', 'show-hero-2')
82+
+makeExample('toh-1/ts/app/app.component.1.ts', 'show-hero-2')
8383
:marked
8484
The browser refreshes and continues to display the hero's name.
8585

@@ -92,56 +92,56 @@ code-example(language="sh" class="code-shell").
9292
change the quotes around the template to backticks
9393
and put the `<h1>`, `<h2>`, and `<div>` elements on their own lines.
9494

95-
+makeExample('toh-1/ts-snippets/app.component.snippets.pt1.ts', 'multi-line-strings', 'app.component.ts (AppComponent\'s template)')
95+
+makeExample('toh-1/ts/app/app.component.1.ts', 'multi-line-strings', 'app.component.ts (AppComponent\'s template)')(format='.')
9696

9797
.l-main-section
9898
:marked
9999
## Editing the hero name
100100

101-
Users should be able to edit the hero name in a textbox.
101+
Users should be able to edit the hero name in an `<input>` textbox.
102+
The textbox should both _display_ the hero's `name` property
103+
and _update_ that property as the user types.
102104

103-
Refactor the hero name `<label>` with `<label>` and `<input>` elements.
105+
You need a two-way binding between the `<input>` form element and the `hero.name` property.
104106

105-
+makeExample('toh-1/ts-snippets/app.component.snippets.pt1.ts', 'editing-Hero', 'app.component.ts (input element)')
107+
### Two-way binding
108+
109+
Refactor the hero name in the template so it looks like this:
110+
+makeExample('toh-1/ts/app/app.component.1.ts', 'name-input')(format='.')
106111
:marked
107-
The hero's name now appears in the `<input>` textbox,
108-
but if you change the name, the change
109-
isn't reflected in the `<h2>`. To get the desired behavior,
110-
you can implement two-way binding.
112+
`[(ngModel)]` is the Angular syntax to bind the `hero.name` property
113+
to the textbox.
114+
Data flow _in both directions_: from the property to the textbox;
115+
and from the textbox back to the property.
111116

112-
### Two-way binding
117+
Unfortunately, immediately after this change, the application breaks.
118+
If you looked in the browser console, you'd see Angular complaining that
119+
" 'ngModel' ... isn't a known property of 'input'."
113120

114-
When users change the name of the hero in the `<input>`,
115-
the changes should appear in any place where the hero's name is bound.
116-
This is known as two-way data binding.
121+
Although `NgModel` is a valid Angular directive, it isn't available by default.
122+
It belongs to the optional `FormsModule`.
123+
You must opt-in to using that module.
117124

118-
Before using two-way data binding for *form inputs*, import the `FormsModule`
119-
package in the Angular module. Add the `FormsModule` to the `NgModule` decorator's `imports` array, which contains the list
120-
of external modules that the app uses.
121-
Now you have included the forms package that includes `ngModel`.
125+
### Import the _FormsModule_
122126

127+
Open the `app.module.ts` file and import the `FormsModule` symbol from the `@angular/forms` library.
128+
Then add the `FormsModule` to the `NgModule` metadata's `imports` array, which contains the list
129+
of external modules that the app uses.
130+
131+
The updated `AppModule` looks like this:
123132
+makeExample('toh-1/ts/app/app.module.ts', '', 'app.module.ts (FormsModule import)')
124133

125134
.l-sub-section
126135
:marked
127136
Read more about `FormsModule` and `ngModel` in the
128-
[Two-way data binding with ngModel](../guide/forms.html#ngModel) section of the
129-
[Forms](../guide/forms.html) page and the
130-
[Two-way binding with NgModel](../guide/template-syntax.html#ngModel) section of the
131-
[Template Syntax](../guide/template-syntax.html)
132-
page.
133-
134-
135-
:marked
136-
Update the template to use the `ngModel` built-in directive for two-way binding.
137-
In app.component.ts, replace the `<input>` with the following HTML:
138-
139-
code-example(language="html").
140-
&lt;input [(ngModel)]="hero.name" placeholder="name">
137+
[_Two-way data binding with ngModel_](../guide/forms.html#ngModel) section of the
138+
[Forms](../guide/forms.html) guide and the
139+
[_Two-way binding with NgModel_](../guide/template-syntax.html#ngModel) section of the
140+
[Template Syntax](../guide/template-syntax.html) guide.
141141

142142
:marked
143-
When the browser refreshes, you can edit the hero's name and
144-
see the changes reflected immediately in the `<h2>`.
143+
When the browser refreshes, the app should work again.
144+
You can edit the hero's name and see the changes reflected immediately in the `<h2>` above the textbox.
145145

146146
.l-main-section
147147
:marked
@@ -150,7 +150,7 @@ code-example(language="html").
150150

151151
* The Tour of Heroes app uses the double curly braces of interpolation (a type of one-way data binding)
152152
to display the app title and properties of a `Hero` object.
153-
* You wrote a multi-line template using ES2015's template strings to make the template readable.
153+
* You wrote a multi-line template using ES2015's template literals to make the template readable.
154154
* You added a two-way data binding to the `<input>` element
155155
using the built-in `ngModel` directive. This binding both displays the hero's name and allows users to change it.
156156
* The `ngModel` directive propagates changes to every other binding of the `hero.name`.
@@ -159,7 +159,7 @@ code-example(language="html").
159159

160160
Here's the complete `app.component.ts` as it stands now:
161161

162-
+makeExample('toh-1/ts/app/app.component.ts', 'pt1', 'app.component.ts')
162+
+makeExample('toh-1/ts/app/app.component.ts', '', 'app.component.ts')
163163

164164
.l-main-section
165165
:marked

0 commit comments

Comments
 (0)