2016-02-06 02:27:06 -05: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 21:04:43 -05:00
|
|
|
|
|
2015-12-14 23:05:13 -05:00
|
|
|
|
:marked
|
2017-03-21 14:08:09 -04:00
|
|
|
|
As the Tour of Heroes app evolves, you'll add more components that need access to hero data.
|
|
|
|
|
|
|
|
|
|
Instead of copying and pasting the same code over and over,
|
|
|
|
|
you'll create a single reusable data service and
|
|
|
|
|
inject it into the components that need it.
|
|
|
|
|
Using a separate service keeps components lean and focused on supporting the view,
|
|
|
|
|
and makes it easy to unit-test components with a mock service.
|
|
|
|
|
|
|
|
|
|
Because data services are invariably asynchronous,
|
|
|
|
|
you'll finish the page with a *!{_Promise}*-based version of the data service.
|
|
|
|
|
|
|
|
|
|
When you're done with this page, the app should look like this <live-example></live-example>.
|
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 21:04:43 -05:00
|
|
|
|
|
2015-12-14 23:05:13 -05:00
|
|
|
|
.l-main-section
|
|
|
|
|
:marked
|
2017-03-21 14:08:09 -04:00
|
|
|
|
## Where you left off
|
|
|
|
|
Before continuing with the Tour of Heroes, verify that you have the following structure.
|
|
|
|
|
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 21:04:43 -05:00
|
|
|
|
|
2015-12-14 23:05:13 -05:00
|
|
|
|
.filetree
|
2016-09-19 23:24:40 -04:00
|
|
|
|
.file angular-tour-of-heroes
|
2015-12-14 23:05:13 -05:00
|
|
|
|
.children
|
2017-02-02 13:38:17 -05:00
|
|
|
|
.file src
|
2015-12-14 23:05:13 -05:00
|
|
|
|
.children
|
2017-02-02 13:38:17 -05:00
|
|
|
|
.file app
|
|
|
|
|
.children
|
|
|
|
|
.file app.component.ts
|
|
|
|
|
.file app.module.ts
|
|
|
|
|
.file hero.ts
|
|
|
|
|
.file hero-detail.component.ts
|
2016-02-11 18:08:06 -05:00
|
|
|
|
.file main.ts
|
2017-02-02 13:38:17 -05:00
|
|
|
|
.file index.html
|
|
|
|
|
.file styles.css
|
|
|
|
|
.file systemjs.config.js
|
|
|
|
|
.file tsconfig.json
|
2016-02-11 18:08:06 -05:00
|
|
|
|
.file node_modules ...
|
2015-12-14 23:05:13 -05:00
|
|
|
|
.file package.json
|
2016-06-07 18:51:25 -04:00
|
|
|
|
|
2015-12-14 23:05:13 -05:00
|
|
|
|
:marked
|
2017-03-21 14:08:09 -04:00
|
|
|
|
## Keep the app transpiling and running
|
|
|
|
|
Enter the following command in the terminal window:
|
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 21:04:43 -05:00
|
|
|
|
|
2016-12-01 11:15:04 -05:00
|
|
|
|
code-example(language="sh" class="code-shell").
|
2015-12-16 21:47:02 -05:00
|
|
|
|
npm start
|
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 21:04:43 -05:00
|
|
|
|
|
2015-12-14 23:05:13 -05:00
|
|
|
|
:marked
|
2017-03-21 14:08:09 -04:00
|
|
|
|
This command runs the TypeScript compiler in "watch mode", recompiling automatically when the code changes.
|
|
|
|
|
The command simultaneously launches the app in a browser and refreshes the browser when the code changes.
|
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 21:04:43 -05:00
|
|
|
|
|
2017-03-21 14:08:09 -04:00
|
|
|
|
You can keep building the Tour of Heroes without pausing to recompile or refresh the browser.
|
2015-12-16 21:47:02 -05:00
|
|
|
|
|
2017-03-21 14:08:09 -04:00
|
|
|
|
## Creating a hero service
|
|
|
|
|
The stakeholders want to show the heroes in various ways on different pages.
|
|
|
|
|
Users can already select a hero from a list.
|
|
|
|
|
Soon you'll add a dashboard with the top performing heroes and create a separate view for editing hero details.
|
|
|
|
|
All three views need hero data.
|
|
|
|
|
|
|
|
|
|
At the moment, the `AppComponent` defines mock heroes for display.
|
|
|
|
|
However, defining heroes is not the component's job,
|
|
|
|
|
and you can't easily share the list of heroes with other components and views.
|
|
|
|
|
In this page, you'll move the hero data acquisition business to a single service that provides the data and
|
|
|
|
|
share that service with all components that need the data.
|
2015-12-16 21:47:02 -05:00
|
|
|
|
|
|
|
|
|
### Create the HeroService
|
2017-03-21 14:08:09 -04:00
|
|
|
|
Create a file in the `app` folder called `hero.service.ts`.
|
2015-12-14 23:05:13 -05: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 21:04:43 -05:00
|
|
|
|
:marked
|
2017-03-21 14:08:09 -04:00
|
|
|
|
The naming convention for service files is the service name in lowercase followed by `.service`.
|
|
|
|
|
For a multi-word service name, use lower [dash-case](../guide/glossary.html#!#dash-case).
|
|
|
|
|
For example, the filename for `SpecialSuperHeroService` is `special-super-hero.service.ts`.
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
2017-03-21 14:08:09 -04:00
|
|
|
|
Name the class `HeroService` and export it for others to import.
|
2015-12-16 21:47:02 -05:00
|
|
|
|
|
2017-02-02 13:38:17 -05:00
|
|
|
|
+makeExample('toh-4/ts/src/app/hero.service.1.ts', 'empty-class', 'src/app/hero.service.ts (starting point)')(format=".")
|
2015-12-14 23:05:13 -05:00
|
|
|
|
|
|
|
|
|
:marked
|
2017-03-21 14:08:09 -04:00
|
|
|
|
### Injectable services
|
|
|
|
|
Notice that you imported the Angular `Injectable` function and applied that function as an `@Injectable()` decorator.
|
2015-12-16 21:47:02 -05:00
|
|
|
|
.callout.is-helpful
|
|
|
|
|
:marked
|
2017-03-21 14:08:09 -04:00
|
|
|
|
Don't forget the parentheses. Omitting them leads to an error that's difficult to diagnose.
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
2017-03-21 14:08:09 -04:00
|
|
|
|
The `@Injectable()` decorator tells TypeScript to emit metadata about the service.
|
|
|
|
|
The metadata specifies that Angular may need to inject other dependencies into this service.
|
2015-12-14 23:05:13 -05:00
|
|
|
|
|
2017-03-21 14:08:09 -04:00
|
|
|
|
Although the `HeroService` doesn't have any dependencies at the moment,
|
|
|
|
|
applying the `@Injectable()` decorator from the start ensures
|
|
|
|
|
consistency and future-proofing.
|
2015-12-14 23:05:13 -05:00
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
2017-03-21 14:08:09 -04:00
|
|
|
|
### Getting hero data
|
2017-03-23 18:47:55 -04:00
|
|
|
|
Add a `getHeroes()` method stub.
|
2017-03-21 14:08:09 -04:00
|
|
|
|
|
2017-02-02 13:38:17 -05:00
|
|
|
|
+makeExample('toh-4/ts/src/app/hero.service.1.ts', 'getHeroes-stub', 'src/app/hero.service.ts (getHeroes stub)')(format=".")
|
2017-03-21 14:08:09 -04:00
|
|
|
|
|
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
2017-03-21 14:08:09 -04:00
|
|
|
|
The `HeroService` could get `Hero` data from anywhere—a
|
|
|
|
|
web service, local storage, or a mock data source.
|
|
|
|
|
Removing data access from the component means
|
|
|
|
|
you can change your mind about the implementation anytime,
|
|
|
|
|
without touching the components that need hero data.
|
2015-12-16 21:47:02 -05:00
|
|
|
|
|
2017-03-21 14:08:09 -04:00
|
|
|
|
### Move the mock hero data
|
2015-12-23 12:42:57 -05:00
|
|
|
|
Cut the `HEROES` array from `app.component.ts` and paste it to a new file in the `app` folder named `mock-heroes.ts`.
|
2017-03-21 14:08:09 -04:00
|
|
|
|
Additionally, copy the `import {Hero} ...` statement because the heroes array uses the `Hero` class.
|
2015-12-16 21:47:02 -05:00
|
|
|
|
|
2017-02-02 13:38:17 -05:00
|
|
|
|
+makeExample('toh-4/ts/src/app/mock-heroes.ts', null, 'src/app/mock-heroes.ts')
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
2017-03-21 14:08:09 -04:00
|
|
|
|
The `HEROES` constant is exported so it can be imported elsewhere, such as the `HeroService`.
|
2015-12-14 23:05:13 -05:00
|
|
|
|
|
2017-03-21 14:08:09 -04:00
|
|
|
|
In `app.component.ts`, where you cut the `HEROES` array,
|
|
|
|
|
add an uninitialized `heroes` property:
|
2017-02-02 13:38:17 -05:00
|
|
|
|
+makeExample('toh-4/ts/src/app/app.component.1.ts', 'heroes-prop', 'src/app/app.component.ts (heroes property)')(format=".")
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
2017-03-21 14:08:09 -04:00
|
|
|
|
### Return mocked hero data
|
2017-03-23 18:47:55 -04:00
|
|
|
|
Back in the `HeroService`, import the mock `HEROES` and return it from the `getHeroes()` method.
|
2017-03-21 14:08:09 -04:00
|
|
|
|
The `HeroService` looks like this:
|
2017-02-02 13:38:17 -05:00
|
|
|
|
+makeExample('toh-4/ts/src/app/hero.service.1.ts', 'full', 'src/app/hero.service.ts')(format=".")
|
2017-03-21 14:08:09 -04:00
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
2017-03-21 14:08:09 -04:00
|
|
|
|
### Import the hero service
|
|
|
|
|
You're ready to use the `HeroService` in other components, starting with `AppComponent`.
|
2015-12-14 23:05:13 -05:00
|
|
|
|
|
2017-03-21 14:08:09 -04:00
|
|
|
|
Import the `HeroService` so that you can reference it in the code.
|
|
|
|
|
+makeExcerpt('toh-4/ts/src/app/app.component.ts', 'hero-service-import', 'src/app/app.component.ts')
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
2017-03-21 14:08:09 -04:00
|
|
|
|
### Don't use *new* with the *HeroService*
|
2015-12-16 21:47:02 -05:00
|
|
|
|
How should the `AppComponent` acquire a runtime concrete `HeroService` instance?
|
2017-03-21 14:08:09 -04:00
|
|
|
|
|
|
|
|
|
You could create a new instance of the `HeroService` with `new` like this:
|
2017-02-02 13:38:17 -05:00
|
|
|
|
+makeExample('toh-4/ts/src/app/app.component.1.ts', 'new-service')(format=".")
|
2017-03-21 14:08:09 -04:00
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
2017-03-21 14:08:09 -04:00
|
|
|
|
However, this option isn't ideal for the following reasons:
|
|
|
|
|
|
|
|
|
|
* The component has to know how to create a `HeroService`.
|
|
|
|
|
If you change the `HeroService` constructor,
|
|
|
|
|
you must find and update every place you created the service.
|
|
|
|
|
Patching code in multiple places is error prone and adds to the test burden.
|
|
|
|
|
* You create a service each time you use `new`.
|
|
|
|
|
What if the service caches heroes and shares that cache with others?
|
|
|
|
|
You couldn't do that.
|
|
|
|
|
* With the `AppComponent` locked into a specific implementation of the `HeroService`,
|
|
|
|
|
switching implementations for different scenarios, such as operating offline or using
|
|
|
|
|
different mocked versions for testing, would be difficult.
|
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
|
|
|
|
|
### Inject the *HeroService*
|
2017-03-21 14:08:09 -04:00
|
|
|
|
|
|
|
|
|
Instead of using the *new* line, you'll add two lines.
|
|
|
|
|
* Add a constructor that also defines a private property.
|
|
|
|
|
* Add to the component's `providers` metadata.
|
|
|
|
|
|
|
|
|
|
Add the constructor:
|
2017-02-02 13:38:17 -05:00
|
|
|
|
+makeExample('toh-4/ts/src/app/app.component.1.ts', 'ctor', 'src/app/app.component.ts (constructor)')
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
2017-03-21 14:08:09 -04:00
|
|
|
|
The constructor itself does nothing. The parameter simultaneously
|
2016-05-03 08:06:32 -04:00
|
|
|
|
defines a private `heroService` property and identifies it as a `HeroService` injection site.
|
2015-12-14 23:05:13 -05:00
|
|
|
|
:marked
|
2017-03-21 14:08:09 -04:00
|
|
|
|
Now Angular knows to supply an instance of the `HeroService` when it creates an `AppComponent`.
|
2015-12-14 23:05:13 -05:00
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
.l-sub-section
|
|
|
|
|
:marked
|
2017-03-21 14:08:09 -04:00
|
|
|
|
Read more about dependency injection in the [Dependency Injection](../guide/dependency-injection.html) page.
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
2017-03-21 14:08:09 -04:00
|
|
|
|
The *injector* doesn't know yet how to create a `HeroService`.
|
|
|
|
|
If you ran the code now, Angular would fail with this error:
|
2016-06-09 15:18:01 -04:00
|
|
|
|
code-example(format="nocode").
|
2015-12-14 23:05:13 -05:00
|
|
|
|
EXCEPTION: No provider for HeroService! (AppComponent -> HeroService)
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
2017-03-21 14:08:09 -04:00
|
|
|
|
To teach the injector how to make a `HeroService`,
|
|
|
|
|
add the following `providers` array property to the bottom of the component metadata
|
2015-12-16 21:47:02 -05:00
|
|
|
|
in the `@Component` call.
|
2015-12-14 23:05:13 -05:00
|
|
|
|
|
2017-03-21 14:08:09 -04:00
|
|
|
|
|
|
|
|
|
+makeExcerpt('toh-4/ts/src/app/app.component.1.ts', 'providers', 'src/app/app.component.ts')
|
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
2017-03-21 14:08:09 -04:00
|
|
|
|
The `providers` array tells Angular to create a fresh instance of the `HeroService` when it creates an `AppComponent`.
|
|
|
|
|
The `AppComponent`, as well as its child components, can use that service to get hero data.
|
2016-05-10 13:49:54 -04:00
|
|
|
|
a#child-component
|
2015-12-14 23:05:13 -05:00
|
|
|
|
:marked
|
2017-03-23 18:47:55 -04:00
|
|
|
|
### *getHeroes()* in the *AppComponent*
|
2017-03-21 14:08:09 -04:00
|
|
|
|
The service is in a `heroService` private variable.
|
|
|
|
|
|
|
|
|
|
You could call the service and get the data in one line.
|
2017-02-02 13:38:17 -05:00
|
|
|
|
+makeExample('toh-4/ts/src/app/app.component.1.ts', 'get-heroes')(format=".")
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
2017-03-21 14:08:09 -04:00
|
|
|
|
You don't really need a dedicated method to wrap one line. Write it anyway:
|
2016-01-23 13:21:09 -05:00
|
|
|
|
|
2017-03-21 14:08:09 -04:00
|
|
|
|
+makeExcerpt('toh-4/ts/src/app/app.component.1.ts', 'getHeroes', 'src/app/app.component.ts')
|
2016-03-16 18:49:55 -04:00
|
|
|
|
<a id="oninit"></a>
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
2017-03-21 14:08:09 -04:00
|
|
|
|
### The *ngOnInit* lifecycle hook
|
|
|
|
|
`AppComponent` should fetch and display hero data with no issues.
|
|
|
|
|
|
2017-03-23 18:47:55 -04:00
|
|
|
|
You might be tempted to call the `getHeroes()` method in a constructor, but
|
2017-03-21 14:08:09 -04:00
|
|
|
|
a constructor should not contain complex logic,
|
|
|
|
|
especially a constructor that calls a server, such as as a data access method.
|
|
|
|
|
The constructor is for simple initializations, like wiring constructor parameters to properties.
|
|
|
|
|
|
2017-03-23 18:47:55 -04:00
|
|
|
|
To have Angular call `getHeroes()`, you can implement the Angular *ngOnInit lifecycle hook*.
|
2017-03-21 14:08:09 -04:00
|
|
|
|
Angular offers interfaces for tapping into critical moments in the component lifecycle:
|
2015-12-16 21:47:02 -05:00
|
|
|
|
at creation, after each change, and at its eventual destruction.
|
2017-03-21 14:08:09 -04:00
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
Each interface has a single method. When the component implements that method, Angular calls it at the appropriate time.
|
|
|
|
|
.l-sub-section
|
|
|
|
|
:marked
|
2017-03-21 14:08:09 -04:00
|
|
|
|
Read more about lifecycle hooks in the [Lifecycle Hooks](../guide/lifecycle-hooks.html) page.
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
2017-03-21 14:08:09 -04:00
|
|
|
|
Here's the essential outline for the `OnInit` interface (don't copy this into your code):
|
|
|
|
|
+makeExample('toh-4/ts/src/app/app.component.1.ts', 'on-init')(format=".")
|
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
2017-03-21 14:08:09 -04:00
|
|
|
|
Add the implementation for the `OnInit` interface to your export statement:
|
|
|
|
|
code-example(format="nocode").
|
|
|
|
|
export class AppComponent implements OnInit {}
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
2017-03-21 14:08:09 -04:00
|
|
|
|
Write an `ngOnInit` method with the initialization logic inside. Angular will call it
|
2017-03-23 18:47:55 -04:00
|
|
|
|
at the right time. In this case, initialize by calling `getHeroes()`.
|
2017-03-21 14:08:09 -04:00
|
|
|
|
+makeExcerpt('toh-4/ts/src/app/app.component.1.ts', 'ng-on-init', 'app/app.component.ts')
|
|
|
|
|
:marked
|
|
|
|
|
The app should run as expected, showing a list of heroes and a hero detail view
|
|
|
|
|
when you click on a hero name.
|
2015-12-16 21:47:02 -05:00
|
|
|
|
|
2017-03-21 14:08:09 -04:00
|
|
|
|
<a id="async"></a>
|
|
|
|
|
:marked
|
|
|
|
|
## Async services and !{_Promise}s
|
|
|
|
|
The `HeroService` returns a list of mock heroes immediately;
|
2017-03-23 18:47:55 -04:00
|
|
|
|
its `getHeroes()` signature is synchronous.
|
2017-02-02 13:38:17 -05:00
|
|
|
|
+makeExample('toh-4/ts/src/app/app.component.1.ts', 'get-heroes')(format=".")
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
2017-03-21 14:08:09 -04:00
|
|
|
|
Eventually, the hero data will come from a remote server.
|
2017-03-23 18:47:55 -04:00
|
|
|
|
When using a remote server, users don't have to wait for the server to respond;
|
|
|
|
|
additionally, you aren't able to block the UI during the wait.
|
2017-03-21 14:08:09 -04:00
|
|
|
|
|
|
|
|
|
:marked
|
2017-03-23 18:47:55 -04:00
|
|
|
|
To coordinate the view with the response,
|
|
|
|
|
you can use *!{_Promise}s*, which is an asynchronous
|
|
|
|
|
technique that changes the signature of the `getHeroes()` method.
|
2017-03-21 14:08:09 -04:00
|
|
|
|
|
|
|
|
|
### The hero service makes a !{_Promise}
|
|
|
|
|
|
|
|
|
|
A *!{_Promise}* essentially promises to call back when the results are ready.
|
|
|
|
|
You ask an asynchronous service to do some work and give it a callback function.
|
|
|
|
|
The service does that work and eventually calls the function with the results or an error.
|
2015-12-16 21:47:02 -05:00
|
|
|
|
.l-sub-section
|
|
|
|
|
:marked
|
2017-03-21 14:08:09 -04:00
|
|
|
|
This is a simplified explanation. Read more about ES2015 Promises in the
|
|
|
|
|
[Promises for asynchronous programming](http://exploringjs.com/es6/ch_promises.html) page of
|
|
|
|
|
[Exploring ES6](http://http://exploringjs.com/es6.html).
|
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
2017-03-23 18:47:55 -04:00
|
|
|
|
Update the `HeroService` with this !{_Promise}-returning `getHeroes()` method:
|
2017-02-02 13:38:17 -05:00
|
|
|
|
+makeExample('toh-4/ts/src/app/hero.service.ts', 'get-heroes', 'src/app/hero.service.ts (excerpt)')(format=".")
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
2017-03-21 14:08:09 -04:00
|
|
|
|
You're still mocking the data. You're simulating the behavior of an ultra-fast, zero-latency server,
|
|
|
|
|
by returning an *immediately resolved !{_Promise}* with the mock heroes as the result.
|
|
|
|
|
|
2016-06-09 15:18:01 -04:00
|
|
|
|
### Act on the !{_Promise}
|
2017-03-21 14:08:09 -04:00
|
|
|
|
|
|
|
|
|
As a result of the change to `HeroService`, `this.heroes` is now set to a !{_Promise} rather than an array of heroes.
|
2017-02-02 13:38:17 -05:00
|
|
|
|
+makeExample('toh-4/ts/src/app/app.component.1.ts', 'getHeroes', 'src/app/app.component.ts (getHeroes - old)')(format=".")
|
2017-03-21 14:08:09 -04:00
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
2017-03-21 14:08:09 -04:00
|
|
|
|
You have to change the implementation to *act on the !{_Promise} when it resolves*.
|
|
|
|
|
When the !{_Promise} resolves successfully, you'll have heroes to display.
|
|
|
|
|
|
2017-03-23 18:47:55 -04:00
|
|
|
|
Pass the callback function as an argument to the !{_Promise}'s `then()` method:
|
2017-02-02 13:38:17 -05:00
|
|
|
|
+makeExample('toh-4/ts/src/app/app.component.ts', 'get-heroes', 'src/app/app.component.ts (getHeroes - revised)')(format=".")
|
2015-12-22 13:25:30 -05:00
|
|
|
|
.l-sub-section
|
|
|
|
|
:marked
|
2017-03-21 14:08:09 -04:00
|
|
|
|
As described in [Arrow functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions),
|
|
|
|
|
the ES2015 arrow function
|
|
|
|
|
in the callback is more succinct than the equivalent function expression and gracefully handles `this`.
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
2017-03-21 14:08:09 -04:00
|
|
|
|
The callback sets the component's `heroes` property to the array of heroes returned by the service.
|
|
|
|
|
|
|
|
|
|
The app is still running, showing a list of heroes, and
|
2015-12-16 21:47:02 -05:00
|
|
|
|
responding to a name selection with a detail view.
|
|
|
|
|
.l-sub-section
|
|
|
|
|
:marked
|
2017-03-21 14:08:09 -04:00
|
|
|
|
At the end of this page, [Appendix: take it slow](#slow) describes what the app might be like with a poor connection.
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
2017-03-21 14:08:09 -04:00
|
|
|
|
## Review the app structure
|
|
|
|
|
Verify that you have the following structure after all of your refactoring:
|
2015-12-14 23:05:13 -05:00
|
|
|
|
|
|
|
|
|
.filetree
|
2016-09-19 23:24:40 -04:00
|
|
|
|
.file angular-tour-of-heroes
|
2015-12-14 23:05:13 -05:00
|
|
|
|
.children
|
2017-02-02 13:38:17 -05:00
|
|
|
|
.file src
|
2015-12-14 23:05:13 -05:00
|
|
|
|
.children
|
2017-02-02 13:38:17 -05:00
|
|
|
|
.file app
|
|
|
|
|
.children
|
|
|
|
|
.file app.component.ts
|
|
|
|
|
.file app.module.ts
|
|
|
|
|
.file hero.ts
|
|
|
|
|
.file hero-detail.component.ts
|
|
|
|
|
.file hero.service.ts
|
|
|
|
|
.file mock-heroes.ts
|
|
|
|
|
.file main.ts
|
|
|
|
|
.file index.html
|
|
|
|
|
.file styles.css
|
|
|
|
|
.file systemjs.config.js
|
|
|
|
|
.file tsconfig.json
|
2017-03-21 14:08:09 -04:00
|
|
|
|
.file node_modules ...
|
2015-12-14 23:05:13 -05:00
|
|
|
|
.file package.json
|
|
|
|
|
:marked
|
2017-03-21 14:08:09 -04:00
|
|
|
|
Here are the code files discussed in this page.
|
2015-12-16 21:47:02 -05:00
|
|
|
|
|
|
|
|
|
+makeTabs(`
|
2017-02-02 13:38:17 -05:00
|
|
|
|
toh-4/ts/src/app/hero.service.ts,
|
|
|
|
|
toh-4/ts/src/app/app.component.ts,
|
|
|
|
|
toh-4/ts/src/app/mock-heroes.ts
|
2015-12-16 21:47:02 -05:00
|
|
|
|
`,'',`
|
2017-02-02 13:38:17 -05:00
|
|
|
|
src/app/hero.service.ts,
|
|
|
|
|
src/app/app.component.ts,
|
|
|
|
|
src/app/mock-heroes.ts
|
2015-12-16 21:47:02 -05:00
|
|
|
|
`)
|
|
|
|
|
:marked
|
2017-03-21 14:08:09 -04:00
|
|
|
|
## The road you've travelled
|
|
|
|
|
Here's what you achieved in this page:
|
|
|
|
|
|
|
|
|
|
* You created a service class that can be shared by many components.
|
|
|
|
|
* You used the `ngOnInit` lifecycle hook to get the hero data when the `AppComponent` activates.
|
|
|
|
|
* You defined the `HeroService` as a provider for the `AppComponent`.
|
|
|
|
|
* You created mock hero data and imported them into the service.
|
|
|
|
|
* You designed the service to return a !{_Promise} and the component to get the data from the !{_Promise}.
|
2015-12-14 23:05:13 -05:00
|
|
|
|
|
2017-03-21 14:08:09 -04:00
|
|
|
|
Your app should look like this <live-example></live-example>.
|
2015-12-16 21:47:02 -05:00
|
|
|
|
|
2017-03-21 14:08:09 -04:00
|
|
|
|
## The road ahead
|
|
|
|
|
The Tour of Heroes has become more reusable using shared components and services.
|
|
|
|
|
The next goal is to create a dashboard, add menu links that route between the views, and format data in a template.
|
2017-03-23 18:47:55 -04:00
|
|
|
|
As the app evolves, you'll discover how to design it to make it easier to grow and maintain.
|
2017-03-21 14:08:09 -04:00
|
|
|
|
|
2017-03-23 18:47:55 -04:00
|
|
|
|
Read about the Angular component router and navigation among the views in the [next tutorial](toh-pt5.html) page.
|
2016-07-03 20:11:17 -04:00
|
|
|
|
|
2015-12-16 21:47:02 -05:00
|
|
|
|
.l-main-section
|
|
|
|
|
<a id="slow"></a>
|
|
|
|
|
:marked
|
2017-03-21 14:08:09 -04:00
|
|
|
|
## Appendix: Take it slow
|
|
|
|
|
To simulate a slow connection,
|
2017-03-23 18:47:55 -04:00
|
|
|
|
import the `Hero` symbol and add the following `getHeroesSlowly()` method to the `HeroService`.
|
2017-03-21 14:08:09 -04:00
|
|
|
|
+makeExample('toh-4/ts/src/app/hero.service.ts', 'get-heroes-slowly', 'app/hero.service.ts (getHeroesSlowly)')(format=".")
|
2015-12-16 21:47:02 -05:00
|
|
|
|
:marked
|
2017-03-23 18:47:55 -04:00
|
|
|
|
Like `getHeroes()`, it also returns a !{_Promise}.
|
2017-03-21 14:08:09 -04:00
|
|
|
|
But this !{_Promise} waits two seconds before resolving the !{_Promise} with mock heroes.
|
|
|
|
|
|
2017-03-23 18:47:55 -04:00
|
|
|
|
Back in the `AppComponent`, replace `getHeroes()` with `getHeroesSlowly()`
|
2015-12-16 21:47:02 -05:00
|
|
|
|
and see how the app behaves.
|