2016-02-05 23:27:06 -08:00
|
|
|
|
include ../_util-fns
|
docs(tutorial): combines all 4 sections + revisions/updates to a.53, hides 3&4
closes #488
ToH History (oldest-to-latest):
----------------------------
created code example/snippets files for use with +makeExample, replace usage of "pre.prettyprint.lang-bash" with this: code-example(format="." language="bash").
fixed spelling errors in examples file path used by +makeExample
changed usage of "code-example" to "+makeExample"
adding code example/snippets files used in toh 1
fixed example file paths, replaced "pre.prettyprint.lang-bash" with "code-example. "
(docs) toh-pt3 initial state
created code examples for display in jade, starting conversion of Google doc and trying +makeExample rendering
all text copied from doc to jade, still some styling and formatting to perform
completed conversion and styling, moved toh3 example files to "tutorial" folder under _examples
created specific code example files for chapter toh 3 and re-pathed references in +makeExample
minor edit
docs) toh combined - initial combined commit
updated ToH for a.52
tons of changes, including de-kebab-ing, removed src folder, updated tsconfig too
fixing snippets using incorrect ending input tag
using inline html and css for the app.component.
ToH Part 1 Code: updated the imports, removed obsolete directive delcarations
ToH Code Part 1: updated to use imports, interface. will hit others soon
toh part 1: ngModel fix
toh part1: removed obsolete story that referred to how we used to have to import and declare all directives we used. yay!
ToH Part 1: updated to use `boot.ts` and `app.component.ts`. this affected the partials, snippets, and the story.
toh part 1: using `npm run go`
toh parts 1 -4: modified all places to use `npm run go`
toh part 1: refactor for jade
toh part 1: fixing the code samples
toh part 2: seeping through the story
toh part 2: fixing snippets.
toh part 2: replaced ngClass with class.selected
toh part 2: removed whitespace
toh part 2: added final state to the code
toh: fixing paths
toh part 4: fixing src/app path to app
toh part 3: fixing folder path
toh part 2: fixed typo
toh part 2: typo on ngModel
toh part 2: added ngif
toh part 2: removed old hero property. moved the details lower, where we need it
toh index: updated hero list image to show consistent styling as the other images here
QS spelling error (targes -> targets)
tweeks: space and ngIF
2015-11-15 18:04:43 -08:00
|
|
|
|
|
2015-12-14 20:05:13 -08:00
|
|
|
|
:marked
|
2017-03-21 11:08:09 -07:00
|
|
|
|
The `AppComponent` is doing _everything_ at the moment.
|
|
|
|
|
In the beginning, it showed details of a single hero.
|
|
|
|
|
Then it became a master/detail form with both a list of heroes and the hero detail.
|
|
|
|
|
Soon there will be new requirements and capabilities.
|
|
|
|
|
You can't keep piling features on top of features in one component; that's not maintainable.
|
2015-12-14 20:05:13 -08:00
|
|
|
|
|
2017-04-01 18:48:01 +01:00
|
|
|
|
You'll need to break it up into sub-components, each focused on a specific task or workflow.
|
2017-03-21 11:08:09 -07:00
|
|
|
|
Eventually, the `AppComponent` could become a simple shell that hosts those sub-components.
|
|
|
|
|
|
|
|
|
|
In this page, you'll take the first step in that direction by carving out the hero details into a separate, reusable component.
|
|
|
|
|
When you're done, the app should look like this <live-example></live-example>.
|
2015-12-14 20:05:13 -08:00
|
|
|
|
|
docs(tutorial): combines all 4 sections + revisions/updates to a.53, hides 3&4
closes #488
ToH History (oldest-to-latest):
----------------------------
created code example/snippets files for use with +makeExample, replace usage of "pre.prettyprint.lang-bash" with this: code-example(format="." language="bash").
fixed spelling errors in examples file path used by +makeExample
changed usage of "code-example" to "+makeExample"
adding code example/snippets files used in toh 1
fixed example file paths, replaced "pre.prettyprint.lang-bash" with "code-example. "
(docs) toh-pt3 initial state
created code examples for display in jade, starting conversion of Google doc and trying +makeExample rendering
all text copied from doc to jade, still some styling and formatting to perform
completed conversion and styling, moved toh3 example files to "tutorial" folder under _examples
created specific code example files for chapter toh 3 and re-pathed references in +makeExample
minor edit
docs) toh combined - initial combined commit
updated ToH for a.52
tons of changes, including de-kebab-ing, removed src folder, updated tsconfig too
fixing snippets using incorrect ending input tag
using inline html and css for the app.component.
ToH Part 1 Code: updated the imports, removed obsolete directive delcarations
ToH Code Part 1: updated to use imports, interface. will hit others soon
toh part 1: ngModel fix
toh part1: removed obsolete story that referred to how we used to have to import and declare all directives we used. yay!
ToH Part 1: updated to use `boot.ts` and `app.component.ts`. this affected the partials, snippets, and the story.
toh part 1: using `npm run go`
toh parts 1 -4: modified all places to use `npm run go`
toh part 1: refactor for jade
toh part 1: fixing the code samples
toh part 2: seeping through the story
toh part 2: fixing snippets.
toh part 2: replaced ngClass with class.selected
toh part 2: removed whitespace
toh part 2: added final state to the code
toh: fixing paths
toh part 4: fixing src/app path to app
toh part 3: fixing folder path
toh part 2: fixed typo
toh part 2: typo on ngModel
toh part 2: added ngif
toh part 2: removed old hero property. moved the details lower, where we need it
toh index: updated hero list image to show consistent styling as the other images here
QS spelling error (targes -> targets)
tweeks: space and ngIF
2015-11-15 18:04:43 -08:00
|
|
|
|
.l-main-section
|
2015-12-14 20:05:13 -08:00
|
|
|
|
:marked
|
2017-03-21 11:08:09 -07:00
|
|
|
|
## Where you left off
|
|
|
|
|
Before getting started on this page, verify that you have the following structure from earlier in the Tour of Heroes.
|
|
|
|
|
If not, go back to the previous pages.
|
docs(tutorial): combines all 4 sections + revisions/updates to a.53, hides 3&4
closes #488
ToH History (oldest-to-latest):
----------------------------
created code example/snippets files for use with +makeExample, replace usage of "pre.prettyprint.lang-bash" with this: code-example(format="." language="bash").
fixed spelling errors in examples file path used by +makeExample
changed usage of "code-example" to "+makeExample"
adding code example/snippets files used in toh 1
fixed example file paths, replaced "pre.prettyprint.lang-bash" with "code-example. "
(docs) toh-pt3 initial state
created code examples for display in jade, starting conversion of Google doc and trying +makeExample rendering
all text copied from doc to jade, still some styling and formatting to perform
completed conversion and styling, moved toh3 example files to "tutorial" folder under _examples
created specific code example files for chapter toh 3 and re-pathed references in +makeExample
minor edit
docs) toh combined - initial combined commit
updated ToH for a.52
tons of changes, including de-kebab-ing, removed src folder, updated tsconfig too
fixing snippets using incorrect ending input tag
using inline html and css for the app.component.
ToH Part 1 Code: updated the imports, removed obsolete directive delcarations
ToH Code Part 1: updated to use imports, interface. will hit others soon
toh part 1: ngModel fix
toh part1: removed obsolete story that referred to how we used to have to import and declare all directives we used. yay!
ToH Part 1: updated to use `boot.ts` and `app.component.ts`. this affected the partials, snippets, and the story.
toh part 1: using `npm run go`
toh parts 1 -4: modified all places to use `npm run go`
toh part 1: refactor for jade
toh part 1: fixing the code samples
toh part 2: seeping through the story
toh part 2: fixing snippets.
toh part 2: replaced ngClass with class.selected
toh part 2: removed whitespace
toh part 2: added final state to the code
toh: fixing paths
toh part 4: fixing src/app path to app
toh part 3: fixing folder path
toh part 2: fixed typo
toh part 2: typo on ngModel
toh part 2: added ngif
toh part 2: removed old hero property. moved the details lower, where we need it
toh index: updated hero list image to show consistent styling as the other images here
QS spelling error (targes -> targets)
tweeks: space and ngIF
2015-11-15 18:04:43 -08:00
|
|
|
|
|
2015-12-14 20:05:13 -08:00
|
|
|
|
.filetree
|
2016-09-20 05:24:40 +02:00
|
|
|
|
.file angular-tour-of-heroes
|
2015-12-14 20:05:13 -08:00
|
|
|
|
.children
|
2017-02-02 18:38:17 +00:00
|
|
|
|
.file src
|
2015-12-14 20:05:13 -08:00
|
|
|
|
.children
|
2017-02-02 18:38:17 +00:00
|
|
|
|
.file app
|
|
|
|
|
.children
|
|
|
|
|
.file app.component.ts
|
|
|
|
|
.file app.module.ts
|
2016-01-28 16:15:26 -08:00
|
|
|
|
.file main.ts
|
2017-02-02 18:38:17 +00:00
|
|
|
|
.file index.html
|
|
|
|
|
.file styles.css
|
|
|
|
|
.file systemjs.config.js
|
|
|
|
|
.file tsconfig.json
|
2017-03-21 11:08:09 -07:00
|
|
|
|
.file node_modules ...
|
2015-12-14 20:05:13 -08:00
|
|
|
|
.file package.json
|
|
|
|
|
:marked
|
2017-03-21 11:08:09 -07:00
|
|
|
|
Keep the app transpiling and running while you build the Tour of Heroes
|
|
|
|
|
by entering the `npm start` command in a terminal window
|
|
|
|
|
[as you did before](toh-pt1.html#keep-transpiling "Keep the app running").
|
docs(tutorial): combines all 4 sections + revisions/updates to a.53, hides 3&4
closes #488
ToH History (oldest-to-latest):
----------------------------
created code example/snippets files for use with +makeExample, replace usage of "pre.prettyprint.lang-bash" with this: code-example(format="." language="bash").
fixed spelling errors in examples file path used by +makeExample
changed usage of "code-example" to "+makeExample"
adding code example/snippets files used in toh 1
fixed example file paths, replaced "pre.prettyprint.lang-bash" with "code-example. "
(docs) toh-pt3 initial state
created code examples for display in jade, starting conversion of Google doc and trying +makeExample rendering
all text copied from doc to jade, still some styling and formatting to perform
completed conversion and styling, moved toh3 example files to "tutorial" folder under _examples
created specific code example files for chapter toh 3 and re-pathed references in +makeExample
minor edit
docs) toh combined - initial combined commit
updated ToH for a.52
tons of changes, including de-kebab-ing, removed src folder, updated tsconfig too
fixing snippets using incorrect ending input tag
using inline html and css for the app.component.
ToH Part 1 Code: updated the imports, removed obsolete directive delcarations
ToH Code Part 1: updated to use imports, interface. will hit others soon
toh part 1: ngModel fix
toh part1: removed obsolete story that referred to how we used to have to import and declare all directives we used. yay!
ToH Part 1: updated to use `boot.ts` and `app.component.ts`. this affected the partials, snippets, and the story.
toh part 1: using `npm run go`
toh parts 1 -4: modified all places to use `npm run go`
toh part 1: refactor for jade
toh part 1: fixing the code samples
toh part 2: seeping through the story
toh part 2: fixing snippets.
toh part 2: replaced ngClass with class.selected
toh part 2: removed whitespace
toh part 2: added final state to the code
toh: fixing paths
toh part 4: fixing src/app path to app
toh part 3: fixing folder path
toh part 2: fixed typo
toh part 2: typo on ngModel
toh part 2: added ngif
toh part 2: removed old hero property. moved the details lower, where we need it
toh index: updated hero list image to show consistent styling as the other images here
QS spelling error (targes -> targets)
tweeks: space and ngIF
2015-11-15 18:04:43 -08:00
|
|
|
|
|
2017-03-21 11:08:09 -07:00
|
|
|
|
## Make a hero detail component
|
|
|
|
|
Add a file named `hero-detail.component.ts` to the `app/` folder.
|
|
|
|
|
This file will hold the new `HeroDetailComponent`.
|
2015-12-14 20:05:13 -08:00
|
|
|
|
|
2017-03-21 11:08:09 -07:00
|
|
|
|
The file and component names follow the standard described in the Angular
|
|
|
|
|
[style guide](../guide/style-guide.html#naming).
|
|
|
|
|
|
|
|
|
|
* The component _class_ name should be written in _upper camel case_ and end in the word "Component".
|
|
|
|
|
The hero detail component class is `HeroDetailComponent`.
|
|
|
|
|
|
|
|
|
|
* The component _file_ name should be spelled in [_lower dash case_](../guide/glossary.html#dash-case),
|
|
|
|
|
each word separated by dashes, and end in `.component.ts`.
|
|
|
|
|
The `HeroDetailComponent` class goes in the `hero-detail.component.ts` file.
|
|
|
|
|
|
|
|
|
|
Start writing the `HeroDetailComponent` as follows:
|
|
|
|
|
|
|
|
|
|
+makeExample('toh-3/ts/app/hero-detail.component.1.ts', 'v1', 'app/hero-detail.component.ts (initial version)')(format=".")
|
|
|
|
|
a#selector
|
2015-12-14 20:05:13 -08:00
|
|
|
|
:marked
|
2017-03-21 11:08:09 -07:00
|
|
|
|
To define a component, you always import the `Component` symbol.
|
|
|
|
|
|
|
|
|
|
The `@Component` decorator provides the Angular metadata for the component.
|
|
|
|
|
The CSS selector name, `hero-detail`, will match the element tag
|
|
|
|
|
that identifies this component within a parent component's template.
|
|
|
|
|
[Near the end of this tutorial page](#add-hero-detail "Add the HeroDetailComponent to the AppComponent"),
|
|
|
|
|
you'll add a `<hero-detail>` element to the `AppComponent` template.
|
|
|
|
|
|
|
|
|
|
Always `export` the component class because you'll always `import` it elsewhere.
|
2015-12-14 20:05:13 -08:00
|
|
|
|
:marked
|
2017-03-21 11:08:09 -07:00
|
|
|
|
### Hero detail template
|
|
|
|
|
To move the hero detail view to the `HeroDetailComponent`,
|
|
|
|
|
cut the hero detail _content_ from the bottom of the `AppComponent` template
|
|
|
|
|
and paste it into a new `template` property in the `@Component` metadata.
|
2015-12-14 20:05:13 -08:00
|
|
|
|
|
2017-03-21 11:08:09 -07:00
|
|
|
|
The `HeroDetailComponent` has a _hero_, not a _selected hero_.
|
|
|
|
|
Replace the word, "selectedHero", with the word, "hero", everywhere in the template.
|
|
|
|
|
When you're done, the new template should look like this:
|
2015-12-14 20:05:13 -08:00
|
|
|
|
|
2017-02-02 18:38:17 +00:00
|
|
|
|
+makeExample('toh-3/ts/src/app/hero-detail.component.ts', 'template', 'src/app/hero-detail.component.ts (template)')(format=".")
|
2015-12-14 20:05:13 -08:00
|
|
|
|
|
2017-03-21 11:08:09 -07:00
|
|
|
|
|
2015-12-14 20:05:13 -08:00
|
|
|
|
:marked
|
2017-03-21 11:08:09 -07:00
|
|
|
|
### Add the *hero* property
|
2015-12-14 20:05:13 -08:00
|
|
|
|
|
2017-03-21 11:08:09 -07:00
|
|
|
|
The `HeroDetailComponent` template binds to the component's `hero` property.
|
|
|
|
|
Add that property to the `HeroDetailComponent` class like this:
|
|
|
|
|
+makeExample('toh-3/ts/app/hero-detail.component.1.ts', 'hero', 'src/app/hero-detail.component.ts (hero property)')
|
2015-12-14 20:05:13 -08:00
|
|
|
|
:marked
|
2017-03-21 11:08:09 -07:00
|
|
|
|
The `hero` property is typed as an instance of `Hero`.
|
|
|
|
|
The `Hero` class is still in the `app.component.ts` file.
|
|
|
|
|
Now there are two components that need to reference the `Hero` class.
|
|
|
|
|
The Angular [style guide](../guide/style-guide.html#rule-of-one "Style guide: rule of one") recommends one class per file anyway.
|
|
|
|
|
|
|
|
|
|
Move the `Hero` class from `app.component.ts` to its own `hero.ts` file.
|
2015-12-14 20:05:13 -08:00
|
|
|
|
|
2017-02-02 18:38:17 +00:00
|
|
|
|
+makeExample('toh-3/ts/src/app/hero.ts', '', 'src/app/hero.ts')(format=".")
|
2015-12-14 20:05:13 -08:00
|
|
|
|
|
|
|
|
|
:marked
|
2017-03-21 11:08:09 -07:00
|
|
|
|
Now that the `Hero` class is in its own file, the `AppComponent` and the `HeroDetailComponent` have to import it.
|
|
|
|
|
Add the following `import` statement near the top of _both_ the `app.component.ts` and the `hero-detail.component.ts` files.
|
|
|
|
|
+makeExample('toh-3/ts/app/hero-detail.component.1.ts', 'hero-import')
|
2015-12-14 20:05:13 -08:00
|
|
|
|
|
|
|
|
|
:marked
|
2017-03-21 11:08:09 -07:00
|
|
|
|
### The *hero* property is an *input* property
|
|
|
|
|
|
|
|
|
|
[Later in this page](#add-hero-detail "Add the HeroDetailComponent to the AppComponent"),
|
|
|
|
|
the parent `AppComponent` will tell the child `HeroDetailComponent` which hero to display
|
|
|
|
|
by binding its `selectedHero` to the `hero` property of the `HeroDetailComponent`.
|
|
|
|
|
The binding will look like this:
|
|
|
|
|
+makeExample('toh-3/ts/app/app.component.1.html', 'hero-detail-binding')(format='.')
|
2015-12-14 20:05:13 -08:00
|
|
|
|
:marked
|
2017-03-21 11:08:09 -07:00
|
|
|
|
Putting square brackets around the `hero` property, to the left of the equal sign (=),
|
|
|
|
|
makes it the *target* of a property binding expression.
|
|
|
|
|
You must declare a *target* binding property to be an *input* property.
|
|
|
|
|
Otherwise, Angular rejects the binding and throws an error.
|
|
|
|
|
|
|
|
|
|
First, amend the `@angular/core` import statement to include the `Input` symbol.
|
|
|
|
|
+makeExample('toh-3/ts/src/app/hero-detail.component.ts', 'import-input', 'src/app/hero-detail.component.ts (excerpt)')(format='.')
|
|
|
|
|
|
2015-12-16 19:38:07 -08:00
|
|
|
|
:marked
|
2017-03-21 11:08:09 -07:00
|
|
|
|
Then declare that `hero` is an *input* property by
|
|
|
|
|
preceding it with the `@Input` decorator that you imported earlier.
|
|
|
|
|
+makeExample('toh-3/ts/src/app/hero-detail.component.ts', 'hero', 'src/app/hero-detail.component.ts (excerpt)')(format='.')
|
2015-12-16 19:38:07 -08:00
|
|
|
|
.l-sub-section
|
|
|
|
|
:marked
|
2017-03-21 11:08:09 -07:00
|
|
|
|
Read more about _input_ properties in the
|
|
|
|
|
[Attribute Directives](../guide/attribute-directives.html#why-input) page.
|
2015-12-14 20:05:13 -08:00
|
|
|
|
|
2017-03-27 16:06:44 +01:00
|
|
|
|
:marked
|
2017-03-21 11:08:09 -07:00
|
|
|
|
That's it. The `hero` property is the only thing in the `HeroDetailComponent` class.
|
|
|
|
|
+makeExample('toh-3/ts/src/app/hero-detail.component.ts', 'class')(format='.')
|
2015-12-14 20:05:13 -08:00
|
|
|
|
:marked
|
2017-03-21 11:08:09 -07:00
|
|
|
|
All it does is receive a hero object through its `hero` input property and then bind to that property with its template.
|
2015-12-14 20:05:13 -08:00
|
|
|
|
|
2017-03-21 11:08:09 -07:00
|
|
|
|
Here's the complete `HeroDetailComponent`.
|
|
|
|
|
+makeExample('toh-3/ts/src/app/hero-detail.component.ts', '', 'src/app/hero-detail.component.ts')
|
2016-08-09 17:38:25 +01:00
|
|
|
|
|
2017-03-21 11:08:09 -07:00
|
|
|
|
.l-main-section
|
2016-08-09 17:38:25 +01:00
|
|
|
|
:marked
|
2017-03-21 11:08:09 -07:00
|
|
|
|
## Declare _HeroDetailComponent_ in the _AppModule_
|
|
|
|
|
Every component must be declared in one—and only one—Angular module.
|
2016-08-09 17:38:25 +01:00
|
|
|
|
|
2017-03-21 11:08:09 -07:00
|
|
|
|
Open `app.module.ts` in your editor and import the `HeroDetailComponent` so you can refer to it.
|
|
|
|
|
+makeExample('toh-3/ts/src/app/app.module.ts', 'hero-detail-import', 'src/app/app.module.ts')
|
2015-12-14 20:05:13 -08:00
|
|
|
|
|
2016-08-09 17:38:25 +01:00
|
|
|
|
:marked
|
2017-03-21 11:08:09 -07:00
|
|
|
|
Add `HeroDetailComponent` to the module's `declarations` array.
|
|
|
|
|
|
|
|
|
|
+makeExample('toh-3/ts/src/app/app.module.ts', 'declarations', 'src/app/app.module.ts')(format='.')
|
2015-12-14 20:05:13 -08:00
|
|
|
|
:marked
|
2017-03-21 11:08:09 -07:00
|
|
|
|
In general, the `declarations` array contains a list of application components, pipes, and directives that belong to the module.
|
|
|
|
|
A component must be declared in a module before other components can reference it.
|
|
|
|
|
This module declares only the two application components, `AppComponent` and `HeroDetailComponent`.
|
2015-12-14 20:05:13 -08:00
|
|
|
|
.l-sub-section
|
docs(tutorial): combines all 4 sections + revisions/updates to a.53, hides 3&4
closes #488
ToH History (oldest-to-latest):
----------------------------
created code example/snippets files for use with +makeExample, replace usage of "pre.prettyprint.lang-bash" with this: code-example(format="." language="bash").
fixed spelling errors in examples file path used by +makeExample
changed usage of "code-example" to "+makeExample"
adding code example/snippets files used in toh 1
fixed example file paths, replaced "pre.prettyprint.lang-bash" with "code-example. "
(docs) toh-pt3 initial state
created code examples for display in jade, starting conversion of Google doc and trying +makeExample rendering
all text copied from doc to jade, still some styling and formatting to perform
completed conversion and styling, moved toh3 example files to "tutorial" folder under _examples
created specific code example files for chapter toh 3 and re-pathed references in +makeExample
minor edit
docs) toh combined - initial combined commit
updated ToH for a.52
tons of changes, including de-kebab-ing, removed src folder, updated tsconfig too
fixing snippets using incorrect ending input tag
using inline html and css for the app.component.
ToH Part 1 Code: updated the imports, removed obsolete directive delcarations
ToH Code Part 1: updated to use imports, interface. will hit others soon
toh part 1: ngModel fix
toh part1: removed obsolete story that referred to how we used to have to import and declare all directives we used. yay!
ToH Part 1: updated to use `boot.ts` and `app.component.ts`. this affected the partials, snippets, and the story.
toh part 1: using `npm run go`
toh parts 1 -4: modified all places to use `npm run go`
toh part 1: refactor for jade
toh part 1: fixing the code samples
toh part 2: seeping through the story
toh part 2: fixing snippets.
toh part 2: replaced ngClass with class.selected
toh part 2: removed whitespace
toh part 2: added final state to the code
toh: fixing paths
toh part 4: fixing src/app path to app
toh part 3: fixing folder path
toh part 2: fixed typo
toh part 2: typo on ngModel
toh part 2: added ngif
toh part 2: removed old hero property. moved the details lower, where we need it
toh index: updated hero list image to show consistent styling as the other images here
QS spelling error (targes -> targets)
tweeks: space and ngIF
2015-11-15 18:04:43 -08:00
|
|
|
|
:marked
|
2017-03-21 11:08:09 -07:00
|
|
|
|
Read more about Angular modules in the [NgModules](../guide/ngmodule.html "Angular Modules (NgModule) guide.
|
|
|
|
|
|
|
|
|
|
a#add-hero-detail
|
|
|
|
|
.l-main-section
|
2015-12-16 19:38:07 -08:00
|
|
|
|
:marked
|
2017-03-21 11:08:09 -07:00
|
|
|
|
## Add the _HeroDetailComponent_ to the _AppComponent_
|
2015-12-16 19:38:07 -08:00
|
|
|
|
:marked
|
2017-03-21 11:08:09 -07:00
|
|
|
|
The `AppComponent` is still a master/detail view.
|
|
|
|
|
It used to display the hero details on its own, before you cut out that portion of the template.
|
|
|
|
|
Now it will delegate to the `HeroDetailComponent`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Recall that `hero-detail` is the CSS [`selector`](#selector "HeroDetailComponent selector")
|
|
|
|
|
in the `HeroDetailComponent` metadata.
|
|
|
|
|
That's the tag name of the element that represents the `HeroDetailComponent`.
|
|
|
|
|
|
|
|
|
|
Add a `<hero-detail>` element near the bottom of the `AppComponent` template,
|
|
|
|
|
where the hero detail view used to be.
|
docs(tutorial): combines all 4 sections + revisions/updates to a.53, hides 3&4
closes #488
ToH History (oldest-to-latest):
----------------------------
created code example/snippets files for use with +makeExample, replace usage of "pre.prettyprint.lang-bash" with this: code-example(format="." language="bash").
fixed spelling errors in examples file path used by +makeExample
changed usage of "code-example" to "+makeExample"
adding code example/snippets files used in toh 1
fixed example file paths, replaced "pre.prettyprint.lang-bash" with "code-example. "
(docs) toh-pt3 initial state
created code examples for display in jade, starting conversion of Google doc and trying +makeExample rendering
all text copied from doc to jade, still some styling and formatting to perform
completed conversion and styling, moved toh3 example files to "tutorial" folder under _examples
created specific code example files for chapter toh 3 and re-pathed references in +makeExample
minor edit
docs) toh combined - initial combined commit
updated ToH for a.52
tons of changes, including de-kebab-ing, removed src folder, updated tsconfig too
fixing snippets using incorrect ending input tag
using inline html and css for the app.component.
ToH Part 1 Code: updated the imports, removed obsolete directive delcarations
ToH Code Part 1: updated to use imports, interface. will hit others soon
toh part 1: ngModel fix
toh part1: removed obsolete story that referred to how we used to have to import and declare all directives we used. yay!
ToH Part 1: updated to use `boot.ts` and `app.component.ts`. this affected the partials, snippets, and the story.
toh part 1: using `npm run go`
toh parts 1 -4: modified all places to use `npm run go`
toh part 1: refactor for jade
toh part 1: fixing the code samples
toh part 2: seeping through the story
toh part 2: fixing snippets.
toh part 2: replaced ngClass with class.selected
toh part 2: removed whitespace
toh part 2: added final state to the code
toh: fixing paths
toh part 4: fixing src/app path to app
toh part 3: fixing folder path
toh part 2: fixed typo
toh part 2: typo on ngModel
toh part 2: added ngif
toh part 2: removed old hero property. moved the details lower, where we need it
toh index: updated hero list image to show consistent styling as the other images here
QS spelling error (targes -> targets)
tweeks: space and ngIF
2015-11-15 18:04:43 -08:00
|
|
|
|
|
2017-03-21 11:08:09 -07:00
|
|
|
|
Coordinate the master `AppComponent` with the `HeroDetailComponent`
|
|
|
|
|
by binding the `selectedHero` property of the `AppComponent`
|
|
|
|
|
to the `hero` property of the `HeroDetailComponent`.
|
|
|
|
|
+makeExample('toh-3/ts/app/app.component.1.html', 'hero-detail-binding', 'app.component.html (excerpt)')(format='.')
|
2015-12-14 20:05:13 -08:00
|
|
|
|
:marked
|
2017-03-21 11:08:09 -07:00
|
|
|
|
Now every time the `selectedHero` changes, the `HeroDetailComponent` gets a new hero to display.
|
|
|
|
|
|
|
|
|
|
The revised `AppComponent` template should look like this:
|
|
|
|
|
|
|
|
|
|
+makeExample('toh-3/ts/src/app/app.component.ts', 'hero-detail-template', 'app.component.ts (excerpt)')(format='.')
|
|
|
|
|
|
|
|
|
|
.l-main-section
|
2015-12-16 19:38:07 -08:00
|
|
|
|
:marked
|
2017-03-21 11:08:09 -07:00
|
|
|
|
## What changed?
|
|
|
|
|
As [before](./toh-pt2.html), whenever a user clicks on a hero name,
|
|
|
|
|
the hero detail appears below the hero list.
|
|
|
|
|
But now the `HeroDetailView` is presenting those details.
|
|
|
|
|
|
|
|
|
|
Refactoring the original `AppComponent` into two components yields benefits, both now and in the future:
|
docs(tutorial): combines all 4 sections + revisions/updates to a.53, hides 3&4
closes #488
ToH History (oldest-to-latest):
----------------------------
created code example/snippets files for use with +makeExample, replace usage of "pre.prettyprint.lang-bash" with this: code-example(format="." language="bash").
fixed spelling errors in examples file path used by +makeExample
changed usage of "code-example" to "+makeExample"
adding code example/snippets files used in toh 1
fixed example file paths, replaced "pre.prettyprint.lang-bash" with "code-example. "
(docs) toh-pt3 initial state
created code examples for display in jade, starting conversion of Google doc and trying +makeExample rendering
all text copied from doc to jade, still some styling and formatting to perform
completed conversion and styling, moved toh3 example files to "tutorial" folder under _examples
created specific code example files for chapter toh 3 and re-pathed references in +makeExample
minor edit
docs) toh combined - initial combined commit
updated ToH for a.52
tons of changes, including de-kebab-ing, removed src folder, updated tsconfig too
fixing snippets using incorrect ending input tag
using inline html and css for the app.component.
ToH Part 1 Code: updated the imports, removed obsolete directive delcarations
ToH Code Part 1: updated to use imports, interface. will hit others soon
toh part 1: ngModel fix
toh part1: removed obsolete story that referred to how we used to have to import and declare all directives we used. yay!
ToH Part 1: updated to use `boot.ts` and `app.component.ts`. this affected the partials, snippets, and the story.
toh part 1: using `npm run go`
toh parts 1 -4: modified all places to use `npm run go`
toh part 1: refactor for jade
toh part 1: fixing the code samples
toh part 2: seeping through the story
toh part 2: fixing snippets.
toh part 2: replaced ngClass with class.selected
toh part 2: removed whitespace
toh part 2: added final state to the code
toh: fixing paths
toh part 4: fixing src/app path to app
toh part 3: fixing folder path
toh part 2: fixed typo
toh part 2: typo on ngModel
toh part 2: added ngif
toh part 2: removed old hero property. moved the details lower, where we need it
toh index: updated hero list image to show consistent styling as the other images here
QS spelling error (targes -> targets)
tweeks: space and ngIF
2015-11-15 18:04:43 -08:00
|
|
|
|
|
2017-03-21 11:08:09 -07:00
|
|
|
|
1. You simplified the `AppComponent` by reducing its responsibilities.
|
2015-12-14 20:05:13 -08:00
|
|
|
|
|
2017-03-21 11:08:09 -07:00
|
|
|
|
1. You can evolve the `HeroDetailComponent` into a rich hero editor
|
|
|
|
|
without touching the parent `AppComponent`.
|
|
|
|
|
|
|
|
|
|
1. You can evolve the `AppComponent` without touching the hero detail view.
|
|
|
|
|
|
|
|
|
|
1. You can re-use the `HeroDetailComponent` in the template of some future parent component.
|
|
|
|
|
|
|
|
|
|
### Review the app structure
|
|
|
|
|
Verify that you have the following structure:
|
2015-12-14 20:05:13 -08:00
|
|
|
|
|
|
|
|
|
.filetree
|
2016-09-20 05:24:40 +02:00
|
|
|
|
.file angular-tour-of-heroes
|
2015-12-14 20:05:13 -08:00
|
|
|
|
.children
|
2017-02-02 18:38:17 +00:00
|
|
|
|
.file src
|
2015-12-14 20:05:13 -08:00
|
|
|
|
.children
|
2017-02-02 18:38:17 +00:00
|
|
|
|
.file app
|
|
|
|
|
.children
|
|
|
|
|
.file app.component.ts
|
|
|
|
|
.file app.module.ts
|
|
|
|
|
.file hero.ts
|
|
|
|
|
.file hero-detail.component.ts
|
2016-02-11 15:08:06 -08:00
|
|
|
|
.file main.ts
|
2017-02-02 18:38:17 +00:00
|
|
|
|
.file index.html
|
|
|
|
|
.file styles.css
|
|
|
|
|
.file systemjs.config.js
|
|
|
|
|
.file tsconfig.json
|
2016-02-11 15:08:06 -08:00
|
|
|
|
.file node_modules ...
|
2015-12-14 20:05:13 -08:00
|
|
|
|
.file package.json
|
2017-03-21 11:08:09 -07:00
|
|
|
|
|
2015-12-16 19:38:07 -08:00
|
|
|
|
:marked
|
2017-03-21 11:08:09 -07:00
|
|
|
|
Here are the code files discussed in this page.
|
2015-12-16 19:38:07 -08:00
|
|
|
|
|
|
|
|
|
+makeTabs(`
|
2017-02-02 18:38:17 +00:00
|
|
|
|
toh-3/ts/src/app/hero-detail.component.ts,
|
|
|
|
|
toh-3/ts/src/app/app.component.ts,
|
|
|
|
|
toh-3/ts/src/app/hero.ts,
|
|
|
|
|
toh-3/ts/src/app/app.module.ts
|
2015-12-16 19:38:07 -08:00
|
|
|
|
`,'',`
|
2017-02-02 18:38:17 +00:00
|
|
|
|
src/app/hero-detail.component.ts,
|
|
|
|
|
src/app/app.component.ts,
|
|
|
|
|
src/app/hero.ts,
|
|
|
|
|
src/app/app.module.ts
|
2016-08-09 17:38:25 +01:00
|
|
|
|
`)
|
2015-12-14 20:05:13 -08:00
|
|
|
|
|
|
|
|
|
.l-main-section
|
|
|
|
|
:marked
|
2017-03-21 11:08:09 -07:00
|
|
|
|
## The road you’ve travelled
|
2017-03-31 15:25:27 -07:00
|
|
|
|
|
2017-03-21 11:08:09 -07:00
|
|
|
|
Here's what you achieved in this page:
|
2015-12-14 20:05:13 -08:00
|
|
|
|
|
2017-03-21 11:08:09 -07:00
|
|
|
|
* You created a reusable component.
|
|
|
|
|
* You learned how to make a component accept input.
|
|
|
|
|
* You learned to declare the required application directives in an Angular module. You
|
|
|
|
|
listed the directives in the `NgModule` decorator's `declarations` array.
|
|
|
|
|
* You learned to bind a parent component to a child component.
|
2015-12-14 20:05:13 -08:00
|
|
|
|
|
2017-03-21 11:08:09 -07:00
|
|
|
|
Your app should look like this <live-example></live-example>.
|
2015-12-14 20:05:13 -08:00
|
|
|
|
|
2015-12-16 19:38:07 -08:00
|
|
|
|
.l-main-section
|
|
|
|
|
:marked
|
2017-03-21 11:08:09 -07:00
|
|
|
|
## The road ahead
|
|
|
|
|
The Tour of Heroes app is more reusable with shared components,
|
|
|
|
|
but its (mock) data is still hard coded within the `AppComponent`.
|
|
|
|
|
That's not sustainable.
|
|
|
|
|
Data access should be refactored to a separate service
|
|
|
|
|
and shared among the components that need data.
|
|
|
|
|
|
|
|
|
|
You’ll learn to create services in the [next tutorial](toh-pt4.html) page.
|