angular-docs-cn/public/docs/_examples/lifecycle-hooks/ts/app/after-content.component.ts

120 lines
2.9 KiB
TypeScript
Raw Normal View History

// #docplaster
// #docregion
import { AfterContentChecked, AfterContentInit, Component, ContentChild } from '@angular/core';
import {LoggerService} from './logger.service';
//////////////////
@Component({
selector: 'my-child',
template: '<input [(ngModel)]="hero">'
})
export class ChildComponent {
hero = 'Magneta';
}
//////////////////////
@Component({
selector: 'after-content',
// #docregion template
2016-04-27 14:28:22 -04:00
template: `
<div>-- projected content begins --</div>
<ng-content></ng-content>
<div>-- projected content ends --</div>`
// #enddocregion template
+ `
<p *ngIf="comment" class="comment">
{{comment}}
</p>
`
})
// #docregion hooks
export class AfterContentComponent implements AfterContentChecked, AfterContentInit {
private prevHero = '';
2016-04-27 14:28:22 -04:00
comment = '';
// Query for a CONTENT child of type `ChildComponent`
@ContentChild(ChildComponent) contentChild: ChildComponent;
// #enddocregion hooks
constructor(private logger: LoggerService) {
this.logIt('AfterContent constructor');
}
// #docregion hooks
ngAfterContentInit() {
// viewChild is set after the view has been initialized
this.logIt('AfterContentInit');
this.doSomething();
}
ngAfterContentChecked() {
// viewChild is updated after the view has been checked
if (this.prevHero === this.contentChild.hero) {
this.logIt('AfterContentChecked (no change)');
} else {
this.prevHero = this.contentChild.hero;
this.logIt('AfterContentChecked');
this.doSomething();
}
}
// #enddocregion hooks
// #docregion do-something
// This surrogate for real business logic sets the `comment`
private doSomething() {
2016-04-27 14:28:22 -04:00
this.comment = this.contentChild.hero.length > 10 ? 'That\'s a long name' : '';
}
private logIt(method: string) {
let vc = this.contentChild;
2016-04-27 14:28:22 -04:00
let message = `${method}: ${vc ? vc.hero : 'no'} child view`;
this.logger.log(message);
}
// #docregion hooks
// ...
}
// #enddocregion hooks
//////////////
@Component({
selector: 'after-content-parent',
template: `
<div class="parent">
<h2>AfterContent</h2>
<div *ngIf="show">` +
// #docregion parent-template
`<after-content>
<my-child></my-child>
</after-content>`
// #enddocregion parent-template
+ `</div>
<h4>-- AfterContent Logs --</h4>
<p><button (click)="reset()">Reset</button></p>
<div *ngFor="let msg of logs">{{msg}}</div>
</div>
`,
styles: ['.parent {background: burlywood}'],
2016-04-27 14:28:22 -04:00
providers: [LoggerService],
directives: [AfterContentComponent, ChildComponent]
})
export class AfterContentParentComponent {
2016-04-27 14:28:22 -04:00
logs: string[];
show = true;
2016-04-27 14:28:22 -04:00
constructor(logger: LoggerService) {
this.logs = logger.logs;
}
reset() {
2016-04-27 14:28:22 -04:00
this.logs.length = 0;
// quickly remove and reload AfterContentComponent which recreates it
this.show = false;
2016-04-27 14:28:22 -04:00
setTimeout(() => this.show = true, 0);
}
}