// #docplaster
// #docregion vc
import { AfterViewInit, ViewChild } from '@angular/core';
// #docregion lv
import { Component } from '@angular/core';
import { CountdownTimerComponent } from './countdown-timer.component';
// #enddocregion lv
// #enddocregion vc
//// Local variable, #timer, version
// #docregion lv
@Component({
selector: 'countdown-parent-lv',
template: `
Countdown to Liftoff (via local variable)
{{timer.seconds}}
`,
directives: [CountdownTimerComponent],
styleUrls: ['demo.css']
})
export class CountdownLocalVarParentComponent { }
// #enddocregion lv
//// View Child version
// #docregion vc
@Component({
selector: 'countdown-parent-vc',
template: `
Countdown to Liftoff (via ViewChild)
{{ seconds() }}
`,
directives: [CountdownTimerComponent],
styleUrls: ['demo.css']
})
export class CountdownViewChildParentComponent implements AfterViewInit {
@ViewChild(CountdownTimerComponent)
private timerComponent: CountdownTimerComponent;
seconds() { return 0; }
ngAfterViewInit() {
// Redefine `seconds()` to get from the `CountdownTimerComponent.seconds` ...
// but wait a tick first to avoid one-time devMode
// unidirectional-data-flow-violation error
setTimeout(() => this.seconds = () => this.timerComponent.seconds, 0);
}
start() { this.timerComponent.start(); }
stop() { this.timerComponent.stop(); }
}
// #enddocregion vc