2016-01-11 07:49:12 -05:00
|
|
|
// Examples of provider arrays
|
2016-01-26 05:00:24 -05:00
|
|
|
//#docplaster
|
2016-01-11 07:49:12 -05:00
|
|
|
import { Component, Host, Inject, Injectable,
|
2016-05-03 08:06:32 -04:00
|
|
|
provide, Provider } from '@angular/core';
|
2016-01-11 07:49:12 -05:00
|
|
|
|
|
|
|
import { APP_CONFIG,
|
2016-05-03 08:06:32 -04:00
|
|
|
Config, 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';
|
|
|
|
import { User, UserService } from './user.service';
|
2016-01-11 07:49:12 -05:00
|
|
|
|
|
|
|
let template = '{{log}}';
|
|
|
|
|
|
|
|
//////////////////////////////////////////
|
|
|
|
@Component({
|
|
|
|
selector: 'provider-1',
|
|
|
|
template: template,
|
|
|
|
providers:
|
|
|
|
//#docregion providers-1
|
|
|
|
[Logger]
|
|
|
|
//#enddocregion providers-1
|
|
|
|
})
|
|
|
|
export class ProviderComponent1 {
|
|
|
|
log:string;
|
|
|
|
constructor(logger: Logger) {
|
|
|
|
logger.log('Hello from logger provided with Logger class');
|
|
|
|
this.log = logger.logs[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////
|
|
|
|
@Component({
|
|
|
|
selector: 'provider-2',
|
|
|
|
template: template,
|
|
|
|
providers:
|
|
|
|
//#docregion providers-2
|
|
|
|
[new Provider(Logger, {useClass: Logger})]
|
|
|
|
//#enddocregion providers-2
|
|
|
|
})
|
|
|
|
export class ProviderComponent2 {
|
|
|
|
log:string;
|
|
|
|
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:
|
|
|
|
//#docregion providers-3
|
|
|
|
[provide(Logger, {useClass: Logger})]
|
|
|
|
//#enddocregion providers-3
|
|
|
|
})
|
|
|
|
export class ProviderComponent3 {
|
|
|
|
log:string;
|
|
|
|
constructor(logger: Logger) {
|
|
|
|
logger.log('Hello from logger provided with useClass');
|
|
|
|
this.log = logger.logs[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////
|
|
|
|
class BetterLogger extends Logger {}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'provider-4',
|
|
|
|
template: template,
|
|
|
|
providers:
|
|
|
|
//#docregion providers-4
|
|
|
|
[provide(Logger, {useClass: BetterLogger})]
|
|
|
|
//#enddocregion providers-4
|
|
|
|
})
|
|
|
|
export class ProviderComponent4 {
|
|
|
|
log:string;
|
|
|
|
constructor(logger: Logger) {
|
|
|
|
logger.log('Hello from logger provided with useClass:BetterLogger');
|
|
|
|
this.log = logger.logs[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////
|
|
|
|
// #docregion EvenBetterLogger
|
|
|
|
@Injectable()
|
|
|
|
class EvenBetterLogger {
|
|
|
|
logs:string[] = [];
|
|
|
|
|
2016-05-03 08:06:32 -04:00
|
|
|
constructor(private userService: UserService) { }
|
2016-01-11 07:49:12 -05:00
|
|
|
|
|
|
|
log(message:string){
|
2016-05-03 08:06:32 -04:00
|
|
|
message = `Message to ${this.userService.user.name}: ${message}.`;
|
2016-01-11 07:49:12 -05:00
|
|
|
console.log(message);
|
|
|
|
this.logs.push(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// #enddocregion EvenBetterLogger
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'provider-5',
|
|
|
|
template: template,
|
|
|
|
providers:
|
|
|
|
//#docregion providers-5
|
2016-01-26 05:00:24 -05:00
|
|
|
[ UserService,
|
|
|
|
provide(Logger, {useClass: EvenBetterLogger}) ]
|
2016-01-11 07:49:12 -05:00
|
|
|
//#enddocregion providers-5
|
|
|
|
})
|
|
|
|
export class ProviderComponent5 {
|
|
|
|
log:string;
|
|
|
|
constructor(logger: Logger) {
|
|
|
|
logger.log('Hello from EvenBetterlogger');
|
|
|
|
this.log = logger.logs[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////
|
|
|
|
class NewLogger extends Logger {}
|
|
|
|
class OldLogger {
|
|
|
|
logs:string[] = [];
|
|
|
|
log(message:string) {
|
|
|
|
throw new Error('Should not call the old logger!')
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'provider-6a',
|
|
|
|
template: template,
|
|
|
|
providers:
|
|
|
|
//#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-01-26 05:00:24 -05:00
|
|
|
provide(OldLogger, {useClass:NewLogger}) ]
|
2016-01-11 07:49:12 -05:00
|
|
|
//#enddocregion providers-6a
|
|
|
|
})
|
|
|
|
export class ProviderComponent6a {
|
|
|
|
log:string;
|
|
|
|
constructor(newLogger:NewLogger, oldLogger: OldLogger) {
|
|
|
|
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:
|
|
|
|
//#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-01-11 07:49:12 -05:00
|
|
|
//#enddocregion providers-6b
|
|
|
|
})
|
|
|
|
export class ProviderComponent6b {
|
|
|
|
log:string;
|
|
|
|
constructor(newLogger:NewLogger, oldLogger: OldLogger) {
|
|
|
|
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: () => {}
|
|
|
|
}
|
|
|
|
// #enddocregion silent-logger
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'provider-7',
|
|
|
|
template: template,
|
|
|
|
providers:
|
|
|
|
//#docregion providers-7
|
|
|
|
[provide(Logger, {useValue: silentLogger})]
|
|
|
|
//#enddocregion providers-7
|
|
|
|
})
|
|
|
|
export class ProviderComponent7 {
|
|
|
|
log:string;
|
|
|
|
constructor(logger: Logger) {
|
|
|
|
logger.log('Hello from logger provided with useValue');
|
|
|
|
this.log = logger.logs[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/////////////////
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'provider-8',
|
|
|
|
template: template,
|
|
|
|
providers:[heroServiceProvider, Logger, UserService]
|
|
|
|
})
|
|
|
|
export class ProviderComponent8{
|
|
|
|
// #docregion provider-8-ctor
|
|
|
|
constructor(heroService: HeroService){ }
|
|
|
|
// #enddocregion provider-8-ctor
|
|
|
|
|
|
|
|
// must be true else this component would have blown up at runtime
|
|
|
|
log = 'Hero service injected successfully'
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////
|
|
|
|
@Component({
|
|
|
|
selector: 'provider-9a',
|
|
|
|
template: template,
|
|
|
|
providers:
|
|
|
|
/*
|
|
|
|
// #docregion providers-9a-interface
|
|
|
|
// FAIL! Can't use interface as provider token
|
|
|
|
[provide(Config, {useValue: CONFIG})]
|
|
|
|
// #enddocregion providers-9a-interface
|
|
|
|
*/
|
|
|
|
// #docregion providers-9a
|
|
|
|
// Use string as provider token
|
|
|
|
[provide('app.config', {useValue: CONFIG})]
|
|
|
|
// #enddocregion providers-9a
|
|
|
|
})
|
|
|
|
export class ProviderComponent9a {
|
|
|
|
log: string;
|
|
|
|
/*
|
|
|
|
// #docregion provider-9a-ctor-interface
|
|
|
|
// FAIL! Can't inject using the interface as the parameter type
|
2016-05-03 08:06:32 -04:00
|
|
|
constructor(private config: Config){ }
|
2016-01-11 07:49:12 -05:00
|
|
|
// #enddocregion provider-9a-ctor-interface
|
|
|
|
*/
|
|
|
|
|
|
|
|
// #docregion provider-9a-ctor
|
|
|
|
// @Inject(token) to inject the dependency
|
2016-05-03 08:06:32 -04:00
|
|
|
constructor(@Inject('app.config') private config: Config){ }
|
2016-01-11 07:49:12 -05:00
|
|
|
// #enddocregion provider-9a-ctor
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'provider-9b',
|
|
|
|
template: template,
|
|
|
|
// #docregion providers-9b
|
|
|
|
providers:[provide(APP_CONFIG, {useValue: CONFIG})]
|
|
|
|
// #enddocregion providers-9b
|
|
|
|
})
|
|
|
|
export class ProviderComponent9b {
|
|
|
|
log: string;
|
|
|
|
// #docregion provider-9b-ctor
|
2016-05-03 08:06:32 -04:00
|
|
|
constructor(@Inject(APP_CONFIG) private config: Config){ }
|
2016-01-11 07:49:12 -05:00
|
|
|
// #enddocregion provider-9b-ctor
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
//////////////////////////////////////////
|
|
|
|
// Normal required logger
|
|
|
|
@Component({
|
|
|
|
selector: 'provider-10a',
|
|
|
|
template: template,
|
|
|
|
//#docregion providers-logger
|
|
|
|
providers: [Logger]
|
|
|
|
//#enddocregion providers-logger
|
|
|
|
})
|
|
|
|
export class ProviderComponent10a {
|
|
|
|
log:string;
|
|
|
|
constructor(logger: Logger) {
|
|
|
|
logger.log('Hello from the required logger.');
|
|
|
|
this.log = logger.logs[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Optional logger
|
|
|
|
// #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
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'provider-10b',
|
|
|
|
template: template
|
|
|
|
})
|
|
|
|
export class ProviderComponent10b {
|
|
|
|
// #docregion provider-10-ctor
|
|
|
|
log:string;
|
2016-05-03 08:06:32 -04:00
|
|
|
constructor(@Optional() private logger:Logger) { }
|
2016-01-11 07:49:12 -05:00
|
|
|
// #enddocregion provider-10-ctor
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
// #docregion provider-10-logger
|
|
|
|
// No logger? Make one!
|
2016-05-03 08:06:32 -04:00
|
|
|
if (!this.logger) {
|
|
|
|
this.logger = {
|
|
|
|
log: (msg:string)=> this.logger.logs.push(msg),
|
2016-01-11 07:49:12 -05:00
|
|
|
logs: []
|
|
|
|
}
|
2016-05-03 08:06:32 -04:00
|
|
|
// #enddocregion provider-10-logger
|
|
|
|
this.logger.log("Optional logger was not available.")
|
|
|
|
// #docregion provider-10-logger
|
2016-01-11 07:49:12 -05:00
|
|
|
}
|
|
|
|
// #enddocregion provider-10-logger
|
|
|
|
else {
|
2016-05-03 08:06:32 -04:00
|
|
|
this.logger.log('Hello from the injected logger.')
|
|
|
|
this.log = this.logger.logs[0];
|
2016-01-11 07:49:12 -05:00
|
|
|
}
|
2016-05-03 08:06:32 -04:00
|
|
|
this.log = this.logger.logs[0];
|
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>
|
|
|
|
<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>
|
|
|
|
<div id="p9a"><provider-9a></provider-9a></div>
|
|
|
|
<div id="p9b"><provider-9b></provider-9b></div>
|
|
|
|
<div id="p10a"><provider-10a></provider-10a></div>
|
|
|
|
<div id="p10b"><provider-10b></provider-10b></div>
|
|
|
|
`,
|
|
|
|
directives:[
|
|
|
|
ProviderComponent1,
|
|
|
|
ProviderComponent2,
|
|
|
|
ProviderComponent3,
|
|
|
|
ProviderComponent4,
|
|
|
|
ProviderComponent5,
|
|
|
|
ProviderComponent6a,
|
|
|
|
ProviderComponent6b,
|
|
|
|
ProviderComponent7,
|
|
|
|
ProviderComponent8,
|
|
|
|
ProviderComponent9a,
|
|
|
|
ProviderComponent9b,
|
|
|
|
ProviderComponent10a,
|
|
|
|
ProviderComponent10b,
|
|
|
|
],
|
|
|
|
})
|
2016-05-03 08:06:32 -04:00
|
|
|
export class ProvidersComponent { }
|