angular-cn/public/docs/_examples/cb-dependency-injection/ts/app/hero-contact.component.ts

41 lines
1.0 KiB
TypeScript
Raw Normal View History

2016-03-26 12:18:13 -04:00
// #docplaster
// #docregion
import { Component, ElementRef, Host, Inject, Optional } from '@angular/core';
import { HeroCacheService } from './hero-cache.service';
import { LoggerService } from './logger.service';
2016-03-26 12:18:13 -04:00
// #docregion component
@Component({
selector:'hero-contact',
template:`
<div>Phone #: {{phoneNumber}}
<span *ngIf="hasLogger">!!!</span></div>`
})
export class HeroContactComponent {
hasLogger = false;
constructor(
// #docregion ctor-params
@Host() // limit to the host component's instance of the HeroCacheService
private heroCache: HeroCacheService,
2016-03-26 12:18:13 -04:00
@Host() // limit search for logger; hides the application-wide logger
@Optional() // ok if the logger doesn't exist
private loggerService: LoggerService
2016-03-26 12:18:13 -04:00
// #enddocregion ctor-params
) {
if (loggerService) {
2016-03-26 12:18:13 -04:00
this.hasLogger = true;
loggerService.logInfo('HeroContactComponent can log!');
2016-03-26 12:18:13 -04:00
}
// #docregion ctor
}
// #enddocregion ctor
get phoneNumber() { return this.heroCache.hero.phone; }
2016-03-26 12:18:13 -04:00
}
// #enddocregion component