parent
5c5c9ca16c
commit
2c8a7982e7
@ -1,16 +1,16 @@
|
|||||||
// #docregion
|
// #docregion
|
||||||
import { BrowserModule } from '@angular/platform-browser';
|
import { BrowserModule } from '@angular/platform-browser';
|
||||||
import { FormsModule } from '@angular/forms';
|
import { FormsModule } from '@angular/forms';
|
||||||
import { XHRBackend } from '@angular/http';
|
import { HttpModule } from '@angular/http';
|
||||||
/* import { routing,
|
|
||||||
appRoutingProviders } from './app.routing';*/
|
/* import { routing } from './app.routing';*/
|
||||||
import { LocationStrategy,
|
import { LocationStrategy,
|
||||||
HashLocationStrategy } from '@angular/common';
|
HashLocationStrategy } from '@angular/common';
|
||||||
import { NgModule } from '@angular/core';
|
import { NgModule } from '@angular/core';
|
||||||
|
|
||||||
import { HeroData } from './hero-data';
|
import { HeroData } from './hero-data';
|
||||||
import { InMemoryBackendService,
|
import { InMemoryWebApiModule } from 'angular2-in-memory-web-api';
|
||||||
SEED_DATA } from 'angular2-in-memory-web-api';
|
|
||||||
|
|
||||||
import { AppComponent } from './app.component';
|
import { AppComponent } from './app.component';
|
||||||
import { HeroBioComponent } from './hero-bio.component';
|
import { HeroBioComponent } from './hero-bio.component';
|
||||||
@ -32,47 +32,43 @@ import { ParentFinderComponent,
|
|||||||
BethComponent,
|
BethComponent,
|
||||||
BobComponent } from './parent-finder.component';
|
BobComponent } from './parent-finder.component';
|
||||||
|
|
||||||
const DIRECTIVES = [
|
const declarations = [
|
||||||
|
AppComponent,
|
||||||
HeroBiosComponent, HeroBiosAndContactsComponent, HeroBioComponent,
|
HeroBiosComponent, HeroBiosAndContactsComponent, HeroBioComponent,
|
||||||
HeroesBaseComponent, SortedHeroesComponent,
|
HeroesBaseComponent, SortedHeroesComponent,
|
||||||
HeroOfTheMonthComponent, HeroContactComponent,
|
HeroOfTheMonthComponent, HeroContactComponent,
|
||||||
HighlightDirective,
|
HighlightDirective,
|
||||||
ParentFinderComponent,
|
ParentFinderComponent,
|
||||||
AppComponent
|
|
||||||
];
|
];
|
||||||
|
|
||||||
const B_DIRECTIVES = [ BarryComponent, BethComponent, BobComponent ];
|
const a_components = [AliceComponent, AlexComponent ];
|
||||||
|
|
||||||
// #docregion C_DIRECTIVES
|
const b_components = [ BarryComponent, BethComponent, BobComponent ];
|
||||||
const C_DIRECTIVES = [
|
|
||||||
|
const c_components = [
|
||||||
CarolComponent, ChrisComponent, CraigComponent,
|
CarolComponent, ChrisComponent, CraigComponent,
|
||||||
CathyComponent
|
CathyComponent
|
||||||
];
|
];
|
||||||
// #enddocregion C_DIRECTIVES
|
|
||||||
|
|
||||||
// #docregion bootstrap
|
|
||||||
@NgModule({
|
@NgModule({
|
||||||
imports: [
|
imports: [
|
||||||
BrowserModule,
|
BrowserModule,
|
||||||
FormsModule,
|
FormsModule,
|
||||||
|
HttpModule,
|
||||||
|
InMemoryWebApiModule.forRoot(HeroData)
|
||||||
// routing TODO: add routes
|
// routing TODO: add routes
|
||||||
],
|
],
|
||||||
declarations: [ ...DIRECTIVES,
|
declarations: [
|
||||||
...B_DIRECTIVES,
|
declarations,
|
||||||
...C_DIRECTIVES,
|
a_components,
|
||||||
AliceComponent,
|
b_components,
|
||||||
AlexComponent ],
|
c_components,
|
||||||
|
],
|
||||||
bootstrap: [ AppComponent ],
|
bootstrap: [ AppComponent ],
|
||||||
|
// #docregion providers
|
||||||
providers: [
|
providers: [
|
||||||
// appRoutingProviders, TODO: add routes
|
{ provide: LocationStrategy, useClass: HashLocationStrategy }
|
||||||
{ provide: LocationStrategy, useClass: HashLocationStrategy },
|
|
||||||
|
|
||||||
{ provide: XHRBackend, useClass: InMemoryBackendService }, // in-mem server
|
|
||||||
{ provide: SEED_DATA, useClass: HeroData } // in-mem server data
|
|
||||||
]
|
]
|
||||||
|
// #enddocregion providers
|
||||||
})
|
})
|
||||||
export class AppModule {
|
export class AppModule { }
|
||||||
constructor() {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// #enddocregion bootstraps
|
|
||||||
|
@ -2,10 +2,11 @@
|
|||||||
import { NgModule } from '@angular/core';
|
import { NgModule } from '@angular/core';
|
||||||
import { BrowserModule } from '@angular/platform-browser';
|
import { BrowserModule } from '@angular/platform-browser';
|
||||||
import { FormsModule } from '@angular/forms';
|
import { FormsModule } from '@angular/forms';
|
||||||
import { HttpModule, JsonpModule, XHRBackend } from '@angular/http';
|
import { HttpModule, JsonpModule } from '@angular/http';
|
||||||
|
|
||||||
import { InMemoryBackendService, SEED_DATA } from 'angular2-in-memory-web-api';
|
import { InMemoryWebApiModule } from 'angular2-in-memory-web-api';
|
||||||
import { HeroData } from './hero-data';
|
import { HeroData } from './hero-data';
|
||||||
|
|
||||||
import { AppComponent } from './app.component';
|
import { AppComponent } from './app.component';
|
||||||
|
|
||||||
import { HeroListComponent } from './toh/hero-list.component';
|
import { HeroListComponent } from './toh/hero-list.component';
|
||||||
@ -19,11 +20,10 @@ import { WikiSmartComponent } from './wiki/wiki-smart.component';
|
|||||||
BrowserModule,
|
BrowserModule,
|
||||||
FormsModule,
|
FormsModule,
|
||||||
HttpModule,
|
HttpModule,
|
||||||
JsonpModule
|
JsonpModule,
|
||||||
],
|
// #docregion in-mem-web-api
|
||||||
providers: [
|
InMemoryWebApiModule.forRoot(HeroData)
|
||||||
{ provide: XHRBackend, useClass: InMemoryBackendService }, // in-mem server
|
// #enddocregion in-mem-web-api
|
||||||
{ provide: SEED_DATA, useClass: HeroData } // in-mem server data
|
|
||||||
],
|
],
|
||||||
declarations: [
|
declarations: [
|
||||||
AppComponent,
|
AppComponent,
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
// #docregion
|
// #docregion
|
||||||
export class HeroData {
|
import { InMemoryDbService } from 'angular2-in-memory-web-api';
|
||||||
|
export class HeroData implements InMemoryDbService {
|
||||||
createDb() {
|
createDb() {
|
||||||
let heroes = [
|
let heroes = [
|
||||||
{ id: '1', name: 'Windstorm' },
|
{ id: '1', name: 'Windstorm' },
|
||||||
|
@ -2,16 +2,14 @@ import { NgModule } from '@angular/core';
|
|||||||
import { browserDynamicPlatform } from '@angular/platform-browser-dynamic';
|
import { browserDynamicPlatform } from '@angular/platform-browser-dynamic';
|
||||||
import { BrowserModule } from '@angular/platform-browser';
|
import { BrowserModule } from '@angular/platform-browser';
|
||||||
|
|
||||||
import { HttpModule,
|
import { HttpModule} from '@angular/http';
|
||||||
XHRBackend } from '@angular/http';
|
import { InMemoryWebApiModule } from 'angular2-in-memory-web-api';
|
||||||
|
|
||||||
import { RouterModule } from '@angular/router';
|
import { RouterModule } from '@angular/router';
|
||||||
|
|
||||||
import { HashLocationStrategy,
|
import { HashLocationStrategy,
|
||||||
LocationStrategy } from '@angular/common';
|
LocationStrategy } from '@angular/common';
|
||||||
|
|
||||||
import { InMemoryBackendService,
|
|
||||||
SEED_DATA } from 'angular2-in-memory-web-api';
|
|
||||||
|
|
||||||
import 'rxjs/add/operator/map';
|
import 'rxjs/add/operator/map';
|
||||||
|
|
||||||
import { HeroData } from './hero-data';
|
import { HeroData } from './hero-data';
|
||||||
@ -49,6 +47,7 @@ const moduleMetadata = {
|
|||||||
imports: [
|
imports: [
|
||||||
BrowserModule,
|
BrowserModule,
|
||||||
HttpModule,
|
HttpModule,
|
||||||
|
InMemoryWebApiModule.forRoot(HeroData),
|
||||||
|
|
||||||
s0101.AppModule,
|
s0101.AppModule,
|
||||||
s0207.AppModule,
|
s0207.AppModule,
|
||||||
@ -77,16 +76,15 @@ const moduleMetadata = {
|
|||||||
s0704.AppModule,
|
s0704.AppModule,
|
||||||
s0901.AppModule,
|
s0901.AppModule,
|
||||||
|
|
||||||
|
|
||||||
RouterModule.forRoot([
|
RouterModule.forRoot([
|
||||||
{ path: '', redirectTo: '/01-01', pathMatch: 'full' }
|
{ path: '', redirectTo: '/01-01', pathMatch: 'full' }
|
||||||
], {/* enableTracing: true */}),
|
], {/* enableTracing: true */}),
|
||||||
],
|
],
|
||||||
declarations: [AppComponent],
|
|
||||||
providers: [
|
providers: [
|
||||||
{ provide: LocationStrategy, useClass: HashLocationStrategy },
|
{ provide: LocationStrategy, useClass: HashLocationStrategy }
|
||||||
{ provide: XHRBackend, useClass: InMemoryBackendService },
|
|
||||||
{ provide: SEED_DATA, useClass: HeroData }
|
|
||||||
],
|
],
|
||||||
|
declarations: [ AppComponent ],
|
||||||
bootstrap: [ AppComponent ]
|
bootstrap: [ AppComponent ]
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -80,25 +80,16 @@ include ../_util-fns
|
|||||||
.l-main-section
|
.l-main-section
|
||||||
:marked
|
:marked
|
||||||
## External module configuration
|
## External module configuration
|
||||||
We can register _certain_ module providers in the `NgModule` rather than in the root application component.
|
We often register providers in the `NgModule` rather than in the root application component.
|
||||||
|
|
||||||
We'd do this when we expect to select or configure external modules that support our application
|
We do this when (a) we expect the service to be injectable everywhere
|
||||||
but (a) aren't conceptually part of the application and (b) that we could change later without
|
or (b) we must configure another application global service _before it starts_.
|
||||||
altering the essential logic of the application.
|
|
||||||
|
|
||||||
For example, we might configure the Component Router with different
|
We see an example of the second case here, where we configure the Component Router with a non-default
|
||||||
[location strategies](../guide/router.html#location-strategy) based on environmental factors.
|
[location strategy](../guide/router.html#location-strategy) by listing its provider
|
||||||
The choice of location strategy doesn't matter to the application itself.
|
in the `providers` list of the `AppModule`.
|
||||||
|
|
||||||
We could sneak in a fake HTTP backend with sample data during development rather than
|
+makeExample('cb-dependency-injection/ts/app/app.module.ts','providers','app/app.module.ts (providers)')(format='.')
|
||||||
allow http calls to a remote server (that might not yet exist).
|
|
||||||
We'll switch to the real backend in production.
|
|
||||||
The application shouldn't know or care one way or the other.
|
|
||||||
|
|
||||||
See both examples in the following `app.module.ts`
|
|
||||||
where we list their service providers in the `providers` array of the `NgModule` (AppModule).
|
|
||||||
|
|
||||||
+makeExample('cb-dependency-injection/ts/app/app.module.ts','bootstrap','app/app.module.ts')(format='.')
|
|
||||||
|
|
||||||
a(id="injectable")
|
a(id="injectable")
|
||||||
a(id="nested-dependencies")
|
a(id="nested-dependencies")
|
||||||
|
@ -669,15 +669,26 @@ a#in-mem-web-api
|
|||||||
Ensure that the `HeroService` endpoint refers to the web API:
|
Ensure that the `HeroService` endpoint refers to the web API:
|
||||||
+makeExample('server-communication/ts/app/toh/hero.service.ts', 'endpoint')(format=".")
|
+makeExample('server-communication/ts/app/toh/hero.service.ts', 'endpoint')(format=".")
|
||||||
:marked
|
:marked
|
||||||
Finally, we need to redirect client HTTP requests to the in-memory web API.
|
Finally, redirect client HTTP requests to the in-memory web API.
|
||||||
block redirect-to-web-api
|
block redirect-to-web-api
|
||||||
:marked
|
:marked
|
||||||
This redirection is easy to configure because Angular's `http` service delegates the client/server communication tasks
|
This redirection is easy to configure with the in-memory web API service module.
|
||||||
|
by adding the `InMemoryWebApiModule` to the `AppModule.imports` list.
|
||||||
|
At the same time, we calling its `forRoot` configuration method with the `HeroData` class.
|
||||||
|
+makeExample('server-communication/ts/app/app.module.ts', 'in-mem-web-api')(format=".")
|
||||||
|
:marked
|
||||||
|
### How it works
|
||||||
|
|
||||||
|
Angular's `http` service delegates the client/server communication tasks
|
||||||
to a helper service called the `XHRBackend`.
|
to a helper service called the `XHRBackend`.
|
||||||
|
|
||||||
To enable our server simulation, we replace the default `XHRBackend` service with
|
Using standard Angular provider registration techniques, the `InMemoryWebApiModule`
|
||||||
the in-memory web API service using standard Angular provider registration techniques.
|
replaces the default `XHRBackend` service with its own in-memory alternative.
|
||||||
We initialize the in-memory web API with *seed data* from the mock hero dataset at the same time.
|
The `forRoot` method initialize the in-memory web API with *seed data* from the mock hero dataset at the same time.
|
||||||
|
.l-sub-section
|
||||||
|
:marked
|
||||||
|
The `forRoot` method name is a strong reminder that you should only call the `InMemoryWebApiModule` _once_
|
||||||
|
while setting the metadata for the root `AppModule`. Don't call it again!.
|
||||||
:marked
|
:marked
|
||||||
Here is the revised (and final) version of <span ngio-ex>app/app.module.ts></span> demonstrating these steps.
|
Here is the revised (and final) version of <span ngio-ex>app/app.module.ts></span> demonstrating these steps.
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user