2016-05-16 21:56:13 -04:00
|
|
|
/* tslint:disable:one-line:check-open-brace*/
|
2016-01-11 07:49:12 -05:00
|
|
|
// Examples of provider arrays
|
2016-05-16 21:56:13 -04:00
|
|
|
// #docplaster
|
|
|
|
import { Component, Inject, Injectable,
|
2016-05-03 08:06:32 -04:00
|
|
|
provide, Provider } from '@angular/core';
|
2016-01-11 07:49:12 -05:00
|
|
|
|
2016-06-03 14:16:46 -04:00
|
|
|
import { APP_CONFIG, AppConfig,
|
|
|
|
HERO_DI_CONFIG } from './app.config';
|
2016-01-11 07:49:12 -05:00
|
|
|
|
2016-05-03 08:06:32 -04:00
|
|
|
import { HeroService } from './heroes/hero.service';
|
|
|
|
import { heroServiceProvider } from './heroes/hero.service.provider';
|
|
|
|
import { Logger } from './logger.service';
|
2016-05-16 21:56:13 -04:00
|
|
|
import { UserService } from './user.service';
|
2016-01-11 07:49:12 -05:00
|
|
|
|
|
|
|
let template = '{{log}}';
|
|
|
|
|
|
|
|
//////////////////////////////////////////
|
|
|
|
@Component({
|
|
|
|
selector: 'provider-1',
|
|
|
|
template: template,
|
2016-06-03 14:16:46 -04:00
|
|
|
// #docregion providers-1, providers-logger
|
|
|
|
providers: [Logger]
|
|
|
|
// #enddocregion providers-1, providers-logger
|
2016-01-11 07:49:12 -05:00
|
|
|
})
|
|
|
|
export class ProviderComponent1 {
|
2016-05-16 21:56:13 -04:00
|
|
|
log: string;
|
2016-01-11 07:49:12 -05:00
|
|
|
constructor(logger: Logger) {
|
|
|
|
logger.log('Hello from logger provided with Logger class');
|
|
|
|
this.log = logger.logs[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////
|
|
|
|
@Component({
|
|
|
|
selector: 'provider-2',
|
|
|
|
template: template,
|
|
|
|
providers:
|
2016-05-16 21:56:13 -04:00
|
|
|
// #docregion providers-2
|
2016-01-11 07:49:12 -05:00
|
|
|
[new Provider(Logger, {useClass: Logger})]
|
2016-05-16 21:56:13 -04:00
|
|
|
// #enddocregion providers-2
|
2016-01-11 07:49:12 -05:00
|
|
|
})
|
|
|
|
export class ProviderComponent2 {
|
2016-05-16 21:56:13 -04:00
|
|
|
log: string;
|
2016-01-11 07:49:12 -05:00
|
|
|
constructor(logger: Logger) {
|
|
|
|
logger.log('Hello from logger provided with Provider class and useClass');
|
|
|
|
this.log = logger.logs[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////
|
|
|
|
@Component({
|
|
|
|
selector: 'provider-3',
|
|
|
|
template: template,
|
|
|
|
providers:
|
2016-05-16 21:56:13 -04:00
|
|
|
// #docregion providers-3
|
2016-01-11 07:49:12 -05:00
|
|
|
[provide(Logger, {useClass: Logger})]
|
2016-05-16 21:56:13 -04:00
|
|
|
// #enddocregion providers-3
|
2016-01-11 07:49:12 -05:00
|
|
|
})
|
|
|
|
export class ProviderComponent3 {
|
2016-05-16 21:56:13 -04:00
|
|
|
log: string;
|
2016-01-11 07:49:12 -05:00
|
|
|
constructor(logger: Logger) {
|
|
|
|
logger.log('Hello from logger provided with useClass');
|
|
|
|
this.log = logger.logs[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-16 21:56:13 -04:00
|
|
|
//////////////////////////////////////////
|
|
|
|
@Component({
|
|
|
|
selector: 'provider-3a',
|
|
|
|
template: template,
|
|
|
|
providers:
|
|
|
|
// #docregion providers-3a
|
|
|
|
[{provide: Logger, useClass: Logger}]
|
|
|
|
// #enddocregion providers-3a
|
|
|
|
})
|
|
|
|
export class ProviderComponent3a {
|
|
|
|
log: string;
|
|
|
|
constructor(logger: Logger) {
|
|
|
|
logger.log('Hello from logger provided with {provide: Logger, useClass: Logger}');
|
|
|
|
this.log = logger.logs[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-11 07:49:12 -05:00
|
|
|
//////////////////////////////////////////
|
|
|
|
class BetterLogger extends Logger {}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'provider-4',
|
|
|
|
template: template,
|
|
|
|
providers:
|
2016-05-16 21:56:13 -04:00
|
|
|
// #docregion providers-4
|
2016-01-11 07:49:12 -05:00
|
|
|
[provide(Logger, {useClass: BetterLogger})]
|
2016-05-16 21:56:13 -04:00
|
|
|
// #enddocregion providers-4
|
2016-01-11 07:49:12 -05:00
|
|
|
})
|
|
|
|
export class ProviderComponent4 {
|
2016-05-16 21:56:13 -04:00
|
|
|
log: string;
|
2016-01-11 07:49:12 -05:00
|
|
|
constructor(logger: Logger) {
|
|
|
|
logger.log('Hello from logger provided with useClass:BetterLogger');
|
|
|
|
this.log = logger.logs[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////
|
|
|
|
// #docregion EvenBetterLogger
|
|
|
|
@Injectable()
|
2016-06-03 14:16:46 -04:00
|
|
|
class EvenBetterLogger extends Logger {
|
|
|
|
constructor(private userService: UserService) { super(); }
|
2016-01-11 07:49:12 -05:00
|
|
|
|
2016-06-03 14:16:46 -04:00
|
|
|
log(message: string) {
|
|
|
|
let name = this.userService.user.name;
|
|
|
|
super.log(`Message to ${name}: ${message}`);
|
2016-01-11 07:49:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// #enddocregion EvenBetterLogger
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'provider-5',
|
|
|
|
template: template,
|
|
|
|
providers:
|
2016-05-16 21:56:13 -04:00
|
|
|
// #docregion providers-5
|
2016-01-26 05:00:24 -05:00
|
|
|
[ UserService,
|
|
|
|
provide(Logger, {useClass: EvenBetterLogger}) ]
|
2016-05-16 21:56:13 -04:00
|
|
|
// #enddocregion providers-5
|
2016-01-11 07:49:12 -05:00
|
|
|
})
|
|
|
|
export class ProviderComponent5 {
|
2016-05-16 21:56:13 -04:00
|
|
|
log: string;
|
2016-01-11 07:49:12 -05:00
|
|
|
constructor(logger: Logger) {
|
|
|
|
logger.log('Hello from EvenBetterlogger');
|
|
|
|
this.log = logger.logs[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////
|
|
|
|
class NewLogger extends Logger {}
|
|
|
|
class OldLogger {
|
2016-05-16 21:56:13 -04:00
|
|
|
logs: string[] = [];
|
|
|
|
log(message: string) {
|
|
|
|
throw new Error('Should not call the old logger!');
|
2016-01-11 07:49:12 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'provider-6a',
|
|
|
|
template: template,
|
|
|
|
providers:
|
2016-05-16 21:56:13 -04:00
|
|
|
// #docregion providers-6a
|
2016-01-26 05:00:24 -05:00
|
|
|
[ NewLogger,
|
2016-01-11 07:49:12 -05:00
|
|
|
// Not aliased! Creates two instances of `NewLogger`
|
2016-05-16 21:56:13 -04:00
|
|
|
provide(OldLogger, {useClass: NewLogger}) ]
|
|
|
|
// #enddocregion providers-6a
|
2016-01-11 07:49:12 -05:00
|
|
|
})
|
|
|
|
export class ProviderComponent6a {
|
2016-05-16 21:56:13 -04:00
|
|
|
log: string;
|
|
|
|
constructor(newLogger: NewLogger, oldLogger: OldLogger) {
|
2016-01-11 07:49:12 -05:00
|
|
|
if (newLogger === oldLogger){
|
|
|
|
throw new Error('expected the two loggers to be different instances');
|
|
|
|
}
|
|
|
|
oldLogger.log('Hello OldLogger (but we want NewLogger)');
|
|
|
|
// The newLogger wasn't called so no logs[]
|
|
|
|
// display the logs of the oldLogger.
|
|
|
|
this.log = newLogger.logs[0] || oldLogger.logs[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'provider-6b',
|
|
|
|
template: template,
|
|
|
|
providers:
|
2016-05-16 21:56:13 -04:00
|
|
|
// #docregion providers-6b
|
2016-01-26 05:00:24 -05:00
|
|
|
[ NewLogger,
|
2016-01-11 07:49:12 -05:00
|
|
|
// Alias OldLogger w/ reference to NewLogger
|
2016-01-26 05:00:24 -05:00
|
|
|
provide(OldLogger, {useExisting: NewLogger}) ]
|
2016-05-16 21:56:13 -04:00
|
|
|
// #enddocregion providers-6b
|
2016-01-11 07:49:12 -05:00
|
|
|
})
|
|
|
|
export class ProviderComponent6b {
|
2016-05-16 21:56:13 -04:00
|
|
|
log: string;
|
|
|
|
constructor(newLogger: NewLogger, oldLogger: OldLogger) {
|
2016-01-11 07:49:12 -05:00
|
|
|
if (newLogger !== oldLogger){
|
|
|
|
throw new Error('expected the two loggers to be the same instance');
|
|
|
|
}
|
|
|
|
oldLogger.log('Hello from NewLogger (via aliased OldLogger)');
|
|
|
|
this.log = newLogger.logs[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////
|
|
|
|
// #docregion silent-logger
|
|
|
|
// An object in the shape of the logger service
|
|
|
|
let silentLogger = {
|
|
|
|
logs: ['Silent logger says "Shhhhh!". Provided via "useValue"'],
|
|
|
|
log: () => {}
|
2016-05-16 21:56:13 -04:00
|
|
|
};
|
2016-01-11 07:49:12 -05:00
|
|
|
// #enddocregion silent-logger
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'provider-7',
|
|
|
|
template: template,
|
|
|
|
providers:
|
2016-05-16 21:56:13 -04:00
|
|
|
// #docregion providers-7
|
2016-01-11 07:49:12 -05:00
|
|
|
[provide(Logger, {useValue: silentLogger})]
|
2016-05-16 21:56:13 -04:00
|
|
|
// #enddocregion providers-7
|
2016-01-11 07:49:12 -05:00
|
|
|
})
|
|
|
|
export class ProviderComponent7 {
|
2016-05-16 21:56:13 -04:00
|
|
|
log: string;
|
2016-01-11 07:49:12 -05:00
|
|
|
constructor(logger: Logger) {
|
|
|
|
logger.log('Hello from logger provided with useValue');
|
|
|
|
this.log = logger.logs[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/////////////////
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'provider-8',
|
|
|
|
template: template,
|
2016-05-16 21:56:13 -04:00
|
|
|
providers: [heroServiceProvider, Logger, UserService]
|
2016-01-11 07:49:12 -05:00
|
|
|
})
|
2016-06-03 14:16:46 -04:00
|
|
|
export class ProviderComponent8 {
|
2016-01-11 07:49:12 -05:00
|
|
|
// #docregion provider-8-ctor
|
2016-06-03 14:16:46 -04:00
|
|
|
constructor(heroService: HeroService) { }
|
2016-01-11 07:49:12 -05:00
|
|
|
// #enddocregion provider-8-ctor
|
|
|
|
|
|
|
|
// must be true else this component would have blown up at runtime
|
2016-06-03 14:16:46 -04:00
|
|
|
log = 'Hero service injected successfully via heroServiceProvider';
|
2016-01-11 07:49:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////
|
|
|
|
@Component({
|
2016-06-03 14:16:46 -04:00
|
|
|
selector: 'provider-9',
|
2016-01-11 07:49:12 -05:00
|
|
|
template: template,
|
|
|
|
/*
|
2016-06-03 14:16:46 -04:00
|
|
|
// #docregion providers-9-interface
|
|
|
|
// FAIL! Can't use interface as provider token
|
|
|
|
[provide(AppConfig, {useValue: HERO_DI_CONFIG})]
|
|
|
|
// #enddocregion providers-9-interface
|
|
|
|
*/
|
|
|
|
// #docregion providers-9
|
|
|
|
providers: [provide(APP_CONFIG, {useValue: HERO_DI_CONFIG})]
|
|
|
|
// #enddocregion providers-9
|
2016-01-11 07:49:12 -05:00
|
|
|
})
|
2016-06-03 14:16:46 -04:00
|
|
|
export class ProviderComponent9 {
|
2016-01-11 07:49:12 -05:00
|
|
|
log: string;
|
2016-06-03 14:16:46 -04:00
|
|
|
/*
|
|
|
|
// #docregion provider-9-ctor-interface
|
|
|
|
// FAIL! Can't inject using the interface as the parameter type
|
|
|
|
constructor(private config: AppConfig){ }
|
|
|
|
// #enddocregion provider-9-ctor-interface
|
|
|
|
*/
|
|
|
|
// #docregion provider-9-ctor
|
|
|
|
constructor(@Inject(APP_CONFIG) private config: AppConfig) { }
|
|
|
|
// #enddocregion provider-9-ctor
|
2016-01-11 07:49:12 -05:00
|
|
|
|
|
|
|
ngOnInit() {
|
2016-05-03 08:06:32 -04:00
|
|
|
this.log = 'APP_CONFIG Application title is ' + this.config.title;
|
2016-01-11 07:49:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//////////////////////////////////////////
|
2016-06-03 14:16:46 -04:00
|
|
|
// Sample providers 1 to 7 illustrate a required logger dependency.
|
|
|
|
// Optional logger, can be null
|
2016-01-11 07:49:12 -05:00
|
|
|
// #docregion import-optional
|
2016-04-27 14:28:22 -04:00
|
|
|
import {Optional} from '@angular/core';
|
2016-01-11 07:49:12 -05:00
|
|
|
// #enddocregion import-optional
|
|
|
|
|
2016-06-03 14:16:46 -04:00
|
|
|
let some_message: string = 'Hello from the injected logger';
|
|
|
|
|
2016-01-11 07:49:12 -05:00
|
|
|
@Component({
|
2016-06-03 14:16:46 -04:00
|
|
|
selector: 'provider-10',
|
2016-01-11 07:49:12 -05:00
|
|
|
template: template
|
|
|
|
})
|
2016-06-03 14:16:46 -04:00
|
|
|
export class ProviderComponent10 {
|
2016-05-16 21:56:13 -04:00
|
|
|
log: string;
|
2016-06-03 14:16:46 -04:00
|
|
|
// #docregion provider-10-ctor
|
|
|
|
constructor(@Optional() private logger: Logger) {
|
|
|
|
if (this.logger)
|
|
|
|
this.logger.log(some_message);
|
|
|
|
}
|
2016-01-11 07:49:12 -05:00
|
|
|
// #enddocregion provider-10-ctor
|
|
|
|
|
|
|
|
ngOnInit() {
|
2016-06-03 14:16:46 -04:00
|
|
|
this.log = this.logger ? this.logger.logs[0] : 'Optional logger was not available';
|
2016-01-11 07:49:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////
|
|
|
|
@Component({
|
|
|
|
selector: 'my-providers',
|
|
|
|
template: `
|
|
|
|
<h2>Provider variations</h2>
|
|
|
|
<div id="p1"><provider-1></provider-1></div>
|
|
|
|
<div id="p2"><provider-2></provider-2></div>
|
|
|
|
<div id="p3"><provider-3></provider-3></div>
|
2016-05-16 21:56:13 -04:00
|
|
|
<div id="p3a"><provider-3a></provider-3a></div>
|
2016-01-11 07:49:12 -05:00
|
|
|
<div id="p4"><provider-4></provider-4></div>
|
|
|
|
<div id="p5"><provider-5></provider-5></div>
|
|
|
|
<div id="p6a"><provider-6a></provider-6a></div>
|
|
|
|
<div id="p6b"><provider-6b></provider-6b></div>
|
|
|
|
<div id="p7"><provider-7></provider-7></div>
|
|
|
|
<div id="p8"><provider-8></provider-8></div>
|
2016-06-03 14:16:46 -04:00
|
|
|
<div id="p9"><provider-9></provider-9></div>
|
|
|
|
<div id="p10"><provider-10></provider-10></div>
|
2016-01-11 07:49:12 -05:00
|
|
|
`,
|
2016-05-16 21:56:13 -04:00
|
|
|
directives: [
|
2016-01-11 07:49:12 -05:00
|
|
|
ProviderComponent1,
|
|
|
|
ProviderComponent2,
|
|
|
|
ProviderComponent3,
|
2016-05-16 21:56:13 -04:00
|
|
|
ProviderComponent3a,
|
2016-01-11 07:49:12 -05:00
|
|
|
ProviderComponent4,
|
|
|
|
ProviderComponent5,
|
|
|
|
ProviderComponent6a,
|
|
|
|
ProviderComponent6b,
|
|
|
|
ProviderComponent7,
|
|
|
|
ProviderComponent8,
|
2016-06-03 14:16:46 -04:00
|
|
|
ProviderComponent9,
|
|
|
|
ProviderComponent10,
|
2016-01-11 07:49:12 -05:00
|
|
|
],
|
|
|
|
})
|
2016-05-03 08:06:32 -04:00
|
|
|
export class ProvidersComponent { }
|