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
|
2016-06-12 18:41:33 -04:00
|
|
|
import { Component, Inject, Injectable, OnInit } 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
|
|
|
})
|
2016-06-12 18:41:33 -04:00
|
|
|
export class Provider1Component {
|
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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-16 21:56:13 -04:00
|
|
|
//////////////////////////////////////////
|
|
|
|
@Component({
|
2016-06-08 12:25:48 -04:00
|
|
|
selector: 'provider-3',
|
2016-05-16 21:56:13 -04:00
|
|
|
template: template,
|
|
|
|
providers:
|
2016-06-08 12:25:48 -04:00
|
|
|
// #docregion providers-3
|
2016-06-03 12:00:53 -04:00
|
|
|
[{ provide: Logger, useClass: Logger }]
|
2016-06-08 12:25:48 -04:00
|
|
|
// #enddocregion providers-3
|
2016-05-16 21:56:13 -04:00
|
|
|
})
|
2016-06-12 18:41:33 -04:00
|
|
|
export class Provider3Component {
|
2016-05-16 21:56:13 -04:00
|
|
|
log: string;
|
|
|
|
constructor(logger: Logger) {
|
2016-06-08 12:25:48 -04:00
|
|
|
logger.log('Hello from logger provided with useClass:Logger');
|
2016-05-16 21:56:13 -04:00
|
|
|
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-06-03 12:00:53 -04:00
|
|
|
[{ provide: Logger, useClass: BetterLogger }]
|
2016-05-16 21:56:13 -04:00
|
|
|
// #enddocregion providers-4
|
2016-01-11 07:49:12 -05:00
|
|
|
})
|
2016-06-12 18:41:33 -04:00
|
|
|
export class Provider4Component {
|
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,
|
2016-06-03 12:00:53 -04:00
|
|
|
{ provide: Logger, useClass: EvenBetterLogger }]
|
2016-05-16 21:56:13 -04:00
|
|
|
// #enddocregion providers-5
|
2016-01-11 07:49:12 -05:00
|
|
|
})
|
2016-06-12 18:41:33 -04:00
|
|
|
export class Provider5Component {
|
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-06-03 12:00:53 -04:00
|
|
|
{ provide: OldLogger, useClass: NewLogger}]
|
2016-05-16 21:56:13 -04:00
|
|
|
// #enddocregion providers-6a
|
2016-01-11 07:49:12 -05:00
|
|
|
})
|
2016-06-12 18:41:33 -04:00
|
|
|
export class Provider6aComponent {
|
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-06-03 12:00:53 -04:00
|
|
|
{ provide: OldLogger, useExisting: NewLogger}]
|
2016-05-16 21:56:13 -04:00
|
|
|
// #enddocregion providers-6b
|
2016-01-11 07:49:12 -05:00
|
|
|
})
|
2016-06-12 18:41:33 -04:00
|
|
|
export class Provider6bComponent {
|
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-06-03 12:00:53 -04:00
|
|
|
[{ provide: Logger, useValue: silentLogger }]
|
2016-05-16 21:56:13 -04:00
|
|
|
// #enddocregion providers-7
|
2016-01-11 07:49:12 -05:00
|
|
|
})
|
2016-06-12 18:41:33 -04:00
|
|
|
export class Provider7Component {
|
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-12 18:41:33 -04:00
|
|
|
export class Provider8Component {
|
|
|
|
// must be true else this component would have blown up at runtime
|
|
|
|
log = 'Hero service injected successfully via heroServiceProvider';
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////
|
|
|
|
@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
|
2016-06-03 12:00:53 -04:00
|
|
|
[{ provide: AppConfig, useValue: HERO_DI_CONFIG })]
|
2016-06-03 14:16:46 -04:00
|
|
|
// #enddocregion providers-9-interface
|
|
|
|
*/
|
|
|
|
// #docregion providers-9
|
2016-06-03 12:00:53 -04:00
|
|
|
providers: [{ provide: APP_CONFIG, useValue: HERO_DI_CONFIG }]
|
2016-06-03 14:16:46 -04:00
|
|
|
// #enddocregion providers-9
|
2016-01-11 07:49:12 -05:00
|
|
|
})
|
2016-06-12 18:41:33 -04:00
|
|
|
export class Provider9Component implements OnInit {
|
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-06-03 12:00:53 -04:00
|
|
|
import { Optional } from '@angular/core';
|
2016-01-11 07:49:12 -05:00
|
|
|
// #enddocregion import-optional
|
|
|
|
|
2016-06-12 18:41:33 -04:00
|
|
|
let some_message = 'Hello from the injected logger';
|
2016-06-03 14:16:46 -04:00
|
|
|
|
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-12 18:41:33 -04:00
|
|
|
export class Provider10Component implements OnInit {
|
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) {
|
2016-06-07 19:06:25 -04:00
|
|
|
if (this.logger) {
|
2016-06-03 14:16:46 -04:00
|
|
|
this.logger.log(some_message);
|
2016-06-07 19:06:25 -04:00
|
|
|
}
|
2016-06-03 14:16:46 -04:00
|
|
|
}
|
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>
|
2016-06-08 12:25:48 -04:00
|
|
|
<div id="p3"><provider-3></provider-3></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-06-12 18:41:33 -04:00
|
|
|
Provider1Component,
|
|
|
|
Provider3Component,
|
|
|
|
Provider4Component,
|
|
|
|
Provider5Component,
|
|
|
|
Provider6aComponent,
|
|
|
|
Provider6bComponent,
|
|
|
|
Provider7Component,
|
|
|
|
Provider8Component,
|
|
|
|
Provider9Component,
|
|
|
|
Provider10Component,
|
2016-01-11 07:49:12 -05:00
|
|
|
],
|
|
|
|
})
|
2016-05-03 08:06:32 -04:00
|
|
|
export class ProvidersComponent { }
|