// #docregion
import {
Component, Input,
OnChanges, SimpleChange,
} from '@angular/core';
import { SpyDirective } from './spy.directive';
import { LoggerService } from './logger.service';
@Component({
selector: 'my-counter',
template: `
Counter = {{counter}}
-- Counter Change Log --
{{chg}}
`,
styles: ['.counter {background: LightYellow; padding: 8px; margin-top: 8px}'],
directives: [SpyDirective]
})
export class MyCounterComponent implements OnChanges {
@Input() counter: number;
changeLog: string[] = [];
ngOnChanges(changes: {[propertyName: string]: SimpleChange}) {
// Empty the changeLog whenever counter goes to zero
// hint: this is a way to respond programmatically to external value changes.
if (this.counter === 0) {
this.changeLog.length = 0;
}
// A change to `counter` is the only change we care about
let chng = changes['counter'];
let cur = chng.currentValue;
let prev = JSON.stringify(chng.previousValue); // first time is {}; after is integer
this.changeLog.push(`counter: currentValue = ${cur}, previousValue = ${prev}`);
}
}
/***************************************/
@Component({
selector: 'counter-parent',
template: `