Add multi use-existing DI demo for Angular 5. · LouisYis/JavaScript-Demos@0ed1dc5 · GitHub
Skip to content

Commit 0ed1dc5

Browse files
committed
Add multi use-existing DI demo for Angular 5.
1 parent 7fae0ec commit 0ed1dc5

19 files changed

Lines changed: 4619 additions & 0 deletions

README.md

Lines changed: 1 addition & 0 deletions
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
# Now that we're using Webpack, we can install modules locally and just ignore
3+
# them since the assets are baked into the compiled modules.
4+
node_modules/
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
:host {
3+
display: block ;
4+
font-size: 18px ;
5+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
// Import the core angular services.
3+
import { Component } from "@angular/core";
4+
import { Inject } from "@angular/core";
5+
6+
// Import the application components and services.
7+
import { Greeter } from "./greeters";
8+
import { GREETERS } from "./greeters";
9+
import { MeanGreeter } from "./greeters";
10+
import { NiceGreeter } from "./greeters";
11+
12+
// ----------------------------------------------------------------------------------- //
13+
// ----------------------------------------------------------------------------------- //
14+
15+
@Component({
16+
selector: "my-app",
17+
styleUrls: [ "./app.component.less" ],
18+
template:
19+
`
20+
<em>Look at console-logging - thats where the hot DI action is.</em>
21+
`
22+
})
23+
export class AppComponent {
24+
25+
// I initialize the app component. Notice that we are injecting BOTH the collection
26+
// of Greeters as well as the individual instances. We can then confirm that the
27+
// collection of Greeters is a mere aggregation of the individual references.
28+
constructor(
29+
@Inject( GREETERS ) greeters: Greeter[],
30+
meanGreeter: MeanGreeter,
31+
niceGreeter: NiceGreeter
32+
) {
33+
34+
console.group( "@Inject( GREETERS )" );
35+
console.log( "Count:", greeters.length );
36+
console.log( greeters[ 0 ] );
37+
console.log( greeters[ 1 ] );
38+
console.groupEnd();
39+
40+
console.group( "MeanGreeter" );
41+
console.log( meanGreeter );
42+
console.log( "=== greeters[ 0 ]:", ( meanGreeter === greeters[ 0 ] ) );
43+
console.log( "=== greeters[ 1 ]:", ( meanGreeter === greeters[ 1 ] ) );
44+
console.groupEnd();
45+
46+
console.group( "NiceGreeter" );
47+
console.log( niceGreeter );
48+
console.log( "=== greeters[ 0 ]:", ( niceGreeter === greeters[ 0 ] ) );
49+
console.log( "=== greeters[ 1 ]:", ( niceGreeter === greeters[ 1 ] ) );
50+
console.groupEnd();
51+
52+
}
53+
54+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
// Import the core angular services.
3+
import { BrowserModule } from "@angular/platform-browser";
4+
import { NgModule } from "@angular/core";
5+
6+
// Import the application components and services.
7+
import { AppComponent } from "./app.component";
8+
import { GREETERS } from "./greeters";
9+
import { MeanGreeter } from "./greeters";
10+
import { NiceGreeter } from "./greeters";
11+
12+
// ----------------------------------------------------------------------------------- //
13+
// ----------------------------------------------------------------------------------- //
14+
15+
@NgModule({
16+
bootstrap: [
17+
AppComponent
18+
],
19+
imports: [
20+
BrowserModule
21+
],
22+
declarations: [
23+
AppComponent
24+
],
25+
providers: [
26+
// First, we're going to provide the greeters as individually injectable
27+
// services. This way, each one can be referenced directly, if needed.
28+
MeanGreeter,
29+
NiceGreeter,
30+
31+
// Next, we're going to provide the AFOREMENTIONED greeters as a collection
32+
// of services. This way, the two services can be injected as a single set of
33+
// services that implement the Greeter interface. By using the "useExisting"
34+
// configuration, the instances associated with the preceding tokens will be
35+
// used, rather than creating new instances of each service.
36+
{
37+
provide: GREETERS,
38+
multi: true,
39+
useExisting: MeanGreeter // ... use instance already in the DI container.
40+
},
41+
{
42+
provide: GREETERS,
43+
multi: true,
44+
useExisting: NiceGreeter // ... use instance already in the DI container.
45+
}
46+
]
47+
})
48+
export class AppModule {
49+
// ...
50+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
// Import the core angular services.
3+
import { InjectionToken } from "@angular/core";
4+
5+
// ----------------------------------------------------------------------------------- //
6+
// ----------------------------------------------------------------------------------- //
7+
8+
// I am the Dependency-Injection (DI) token for the Greeter collection.
9+
export var GREETERS = new InjectionToken<Greeter[]>( "Greeter[] Multi Token" );
10+
11+
export interface Greeter {
12+
greet( name: string ) : string;
13+
}
14+
15+
export class NiceGreeter implements Greeter {
16+
17+
public greet( name: string ) : string {
18+
19+
return( `Hello ${ name }, so nice to meet you.` );
20+
21+
}
22+
23+
}
24+
25+
export class MeanGreeter implements Greeter {
26+
27+
public greet( name: string ) : string {
28+
29+
return( `What evs ${ name }, talk to the hand!` );
30+
31+
}
32+
33+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
6+
<title>
7+
Providing Services As Both A Multi-Collection And As An Individual Injectable In Angular 5.1.0
8+
</title>
9+
</head>
10+
<body>
11+
12+
<h1>
13+
Providing Services As Both A Multi-Collection And As An Individual Injectable In Angular 5.1.0
14+
</h1>
15+
16+
<my-app>
17+
<p>
18+
<em>Loading files...</em>
19+
</p>
20+
21+
<p>
22+
npm Run Scripts:
23+
</p>
24+
25+
<ul>
26+
<li>
27+
<strong>npm run build</strong> &mdash; Compiles the .ts file into bundles.
28+
</li>
29+
<li>
30+
<strong>npm run watch</strong> &mdash; Compiles the .ts file into bundles and then watches files for changes.
31+
</li>
32+
</ul>
33+
</my-app>
34+
35+
</body>
36+
</html>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
// Import these libraries for their side-effects.
3+
import "core-js/client/shim.min.js";
4+
import "zone.js/dist/zone.js";
5+
6+
// Load the Web Animations API polyfill for most browsers (basically any browser other than Chrome and Firefox).
7+
// import "web-animations-js/web-animations.min.js";
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
// Import the core angular services.
3+
import { platformBrowserDynamic } from "@angular/platform-browser-dynamic";
4+
5+
// Import the root module for bootstrapping.
6+
import { AppModule } from "./app.module";
7+
8+
platformBrowserDynamic().bootstrapModule( AppModule );
Lines changed: 8 additions & 0 deletions

0 commit comments

Comments
 (0)