// #docregion
import {
Component, Input, Output,
OnChanges, SimpleChange,
} from 'angular2/core';
export class Hero {
constructor(public name:string){}
}
@Component({
selector: 'my-hero',
template: `
{{hero.name}} can {{power}}
-- Change Log --
{{chg}}
`,
styles: [
'.hero {background: LightYellow; padding: 8px; margin-top: 8px}',
'p {background: Yellow; padding: 8px; margin-top: 8px}'
]
})
export class MyHeroComponent implements OnChanges {
@Input() hero: Hero;
@Input() power: string;
@Input() reset: {};
changeLog:string[] = [];
ngOnChanges(changes: {[propertyName: string]: SimpleChange}) {
// Empty the changeLog whenever 'reset' property changes
// hint: this is a way to respond programmatically to external value changes.
if (changes['reset']) { this.changeLog.length = 0; }
for (let propName in changes) {
let prop = changes[propName];
let cur = JSON.stringify(prop.currentValue)
let prev = JSON.stringify(prop.previousValue); // first time is {}; after is integer
this.changeLog.push(`${propName}: currentValue = ${cur}, previousValue = ${prev}`);
}
}
}
/***************************************/
@Component({
selector: 'on-changes-parent',
template: `
`,
styles: ['.parent {background: Lavender; padding: 10px; margin:100px 8px;}'],
directives: [MyHeroComponent]
})
export class OnChangesParentComponent {
hero:Hero;
power:string;
resetTrigger = false;
constructor() {
this.reset();
}
reset(){
// new Hero object every time; triggers onChange
this.hero = new Hero('Windstorm');
// setting power only triggers onChange if this value is different
this.power = 'sing';
// always triggers onChange ... which is interpreted as a reset
this.resetTrigger = !this.resetTrigger;
}
}