// #docregion
import {
Component, Input, Output,
AfterContentChecked, AfterContentInit, ContentChild,
AfterViewInit, ViewChild
} from 'angular2/core';
import {ChildComponent} from './child.component';
import {LoggerService} from './logger.service';
@Component({
selector: 'after-content',
template: `
-- child content begins --
-- child content ends --
`,
styles: ['.after-content {background: LightCyan; padding: 8px;}'],
})
export class AfterContentComponent
implements AfterContentChecked, AfterContentInit, AfterViewInit {
private _logger:LoggerService;
constructor(logger:LoggerService){
this._logger = logger;
logger.log('AfterContent ctor: ' + this._getMessage());
}
// Query for a CONTENT child of type `ChildComponent`
@ContentChild(ChildComponent) contentChild: ChildComponent;
// Query for a VIEW child of type`ChildComponent`
// No such VIEW child exists!
// This component holds content but no view of that type.
@ViewChild(ChildComponent) viewChild: ChildComponent;
///// Hooks
ngAfterContentInit() {
// contentChild is set after the content has been initialized
this._logger.log('AfterContentInit: ' + this._getMessage());
}
ngAfterViewInit() {
this._logger.log(`AfterViewInit: There is ${this.viewChild ? 'a' : 'no'} view child`);
}
private _prevHero:string;
ngAfterContentChecked() {
// contentChild is updated after the content has been checked
// Called frequently; only report when the hero changes
if (!this.contentChild || this._prevHero === this.contentChild.hero) {return;}
this._prevHero = this.contentChild.hero;
this._logger.log('AfterContentChecked: ' + this._getMessage());
}
private _getMessage(): string {
let cmp = this.contentChild;
return cmp ? `"${cmp.hero}" child content` : 'no child content';
}
}
/***************************************/
@Component({
selector: 'after-content-parent',
template: `