Skip to content

Commit 94eb9ce

Browse files
wardbellFoxandxss
authored andcommitted
docs: Dependency Injection guides for CLI
1 parent 24cf8b3 commit 94eb9ce

25 files changed

Lines changed: 609 additions & 879 deletions

‎aio/content/examples/dependency-injection/plnkr.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"files":[
55
"!**/*.d.ts",
66
"!**/*.js",
7-
"!**/*.[1,2].*"
7+
"!**/*.[0,1,2].*",
8+
"**/dummy.module.ts"
89
],
910
"tags": ["dependency", "di"]
1011
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
Must put this interface in its own file instead of app.config.ts
3+
or else TypeScript gives a (bogus) warning:
4+
WARNING in ./src/app/... .ts
5+
"export 'AppConfig' was not found in './app.config'
6+
*/
7+
export interface AppConfig {
8+
apiEndpoint: string;
9+
title: string;
10+
}

‎aio/content/examples/dependency-injection/src/app/app.component.1.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
// Early versions
2-
31
// #docregion
4-
import { Component } from '@angular/core';
2+
import { Component } from '@angular/core';
53

64
@Component({
75
selector: 'app-root',

‎aio/content/examples/dependency-injection/src/app/app.component.2.ts‎

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

65
import { APP_CONFIG, AppConfig } from './app.config';
76
// #enddocregion imports
@@ -23,3 +22,5 @@ export class AppComponent {
2322
}
2423
// #enddocregion ctor
2524
}
25+
// #enddocregion
26+

‎aio/content/examples/dependency-injection/src/app/app.component.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import { Component, Inject } from '@angular/core';
55

66
import { APP_CONFIG, AppConfig } from './app.config';
7-
import { Logger } from './logger.service';
87
import { UserService } from './user.service';
98
// #enddocregion imports
109

@@ -23,8 +22,7 @@ import { UserService } from './user.service';
2322
<app-heroes id="authorized" *ngIf="isAuthorized"></app-heroes>
2423
<app-heroes id="unauthorized" *ngIf="!isAuthorized"></app-heroes>
2524
<app-providers></app-providers>
26-
`,
27-
providers: [Logger]
25+
`
2826
})
2927
export class AppComponent {
3028
title: string;

‎aio/content/examples/dependency-injection/src/app/app.config.ts‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1+
import { AppConfig } from './app-config';
2+
export { AppConfig } from './app-config';
3+
14
// #docregion token
25
import { InjectionToken } from '@angular/core';
36

4-
export let APP_CONFIG = new InjectionToken<AppConfig>('app.config');
7+
export const APP_CONFIG = new InjectionToken<AppConfig>('app.config');
58
// #enddocregion token
69

710
// #docregion config
8-
export interface AppConfig {
9-
apiEndpoint: string;
10-
title: string;
11-
}
12-
1311
export const HERO_DI_CONFIG: AppConfig = {
1412
apiEndpoint: 'api.heroes.com',
1513
title: 'Dependency Injection'
Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,24 @@
1+
// #docplaster
12
import { NgModule } from '@angular/core';
23
import { BrowserModule } from '@angular/platform-browser';
34

5+
import { APP_CONFIG, HERO_DI_CONFIG } from './app.config';
46
import { AppComponent } from './app.component';
57
import { CarComponent } from './car/car.component';
68
import { HeroesComponent } from './heroes/heroes.component';
79
import { HeroListComponent } from './heroes/hero-list.component';
810
import { InjectorComponent } from './injector.component';
11+
import { Logger } from './logger.service';
912
import { TestComponent } from './test.component';
10-
import { APP_CONFIG, HERO_DI_CONFIG } from './app.config';
1113
import { UserService } from './user.service';
12-
import {
13-
ProvidersComponent,
14-
Provider1Component,
15-
Provider3Component,
16-
Provider4Component,
17-
Provider5Component,
18-
Provider6aComponent,
19-
Provider6bComponent,
20-
Provider7Component,
21-
Provider8Component,
22-
Provider9Component,
23-
Provider10Component,
24-
} from './providers.component';
14+
15+
import { ProvidersModule } from './providers.module';
2516

2617
// #docregion ngmodule
2718
@NgModule({
2819
imports: [
29-
BrowserModule
20+
BrowserModule,
21+
ProvidersModule
3022
],
3123
declarations: [
3224
AppComponent,
@@ -35,26 +27,19 @@ import {
3527
// #enddocregion ngmodule
3628
HeroListComponent,
3729
InjectorComponent,
38-
TestComponent,
39-
ProvidersComponent,
40-
Provider1Component,
41-
Provider3Component,
42-
Provider4Component,
43-
Provider5Component,
44-
Provider6aComponent,
45-
Provider6bComponent,
46-
Provider7Component,
47-
Provider8Component,
48-
Provider9Component,
49-
Provider10Component,
30+
TestComponent
5031
// #docregion ngmodule
5132
],
52-
// #docregion ngmodule-providers
33+
// #docregion providers, providers-2
5334
providers: [
35+
// #enddocregion providers
36+
Logger,
37+
// #docregion providers
5438
UserService,
5539
{ provide: APP_CONFIG, useValue: HERO_DI_CONFIG }
5640
],
57-
// #enddocregion ngmodule-providers
41+
// #enddocregion providers, providers-2
42+
exports: [ CarComponent, HeroesComponent ],
5843
bootstrap: [ AppComponent ]
5944
})
6045
export class AppModule { }
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
/// Dummy modules to satisfy Angular Language Service
3+
import { NgModule } from '@angular/core';
4+
import { CommonModule } from '@angular/common';
5+
import { AppModule } from './app.module';
6+
7+
////////
8+
9+
import { AppComponent as AppComponent1 } from './app.component.1';
10+
11+
@NgModule({
12+
imports: [ CommonModule, AppModule ],
13+
declarations: [ AppComponent1 ]
14+
})
15+
export class DummyModule1 {}
16+
17+
/////////
18+
19+
import { AppComponent as AppComponent2 } from './app.component.2';
20+
21+
@NgModule({
22+
imports: [ CommonModule, AppModule ],
23+
declarations: [ AppComponent2 ]
24+
})
25+
export class DummyModule2 {}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
/// Dummy modules to satisfy Angular Language Service
3+
import { NgModule } from '@angular/core';
4+
import { CommonModule } from '@angular/common';
5+
6+
////////
7+
8+
import { HeroListComponent as HeroListComponent1 } from './hero-list.component.1';
9+
10+
@NgModule({
11+
imports: [ CommonModule ],
12+
declarations: [ HeroListComponent1 ],
13+
exports: [ HeroListComponent1 ]
14+
})
15+
export class DummyModule1 {}
16+
17+
/////////
18+
19+
import { HeroListComponent as HeroListComponent2 } from './hero-list.component.2';
20+
21+
@NgModule({
22+
imports: [ CommonModule ],
23+
declarations: [ HeroListComponent2 ]
24+
})
25+
export class DummyModule2 {}
26+
27+
/////////
28+
29+
import { HeroesComponent as HeroesComponent1 } from './heroes.component.1';
30+
31+
@NgModule({
32+
imports: [ CommonModule, DummyModule1 ],
33+
declarations: [ HeroesComponent1 ]
34+
})
35+
export class DummyModule3 {}
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
// #docregion
22
import { Component } from '@angular/core';
3-
43
import { HEROES } from './mock-heroes';
54

65
@Component({
76
selector: 'app-hero-list',
87
template: `
9-
<div *ngFor="let hero of heroes">
10-
{{hero.id}} - {{hero.name}}
11-
</div>
8+
<div *ngFor="let hero of heroes">
9+
{{hero.id}} - {{hero.name}}
10+
</div>
1211
`
1312
})
13+
// #docregion class
1414
export class HeroListComponent {
1515
heroes = HEROES;
1616
}
17+
// #enddocregion class

0 commit comments

Comments
 (0)