Patrice Chalin ac92e77611 docs(dev guide): lifecycle-hooks - updated dart/ts code and new dart prose
Mainly copyedits, but also

- Dart .jade extends TS .jade file with minor overrides
- Significant update of example code (so it matches the ts example in its
appearance and behavior).
- Tweaks to Dart code.
- A few extra/corrected mixin definitions in `_util-fns.jade`.
2016-05-06 14:17:34 +01:00

76 lines
1.8 KiB
TypeScript

/* tslint:disable:forin */
// #docregion
import {
Component, Input, OnChanges,
SimpleChange, ViewChild
} from '@angular/core';
class Hero {
constructor(public name: string) {}
}
@Component({
selector: 'on-changes',
template: `
<div class="hero">
<p>{{hero.name}} can {{power}}</p>
<h4>-- Change Log --</h4>
<div *ngFor="let chg of changeLog">{{chg}}</div>
</div>
`,
styles: [
'.hero {background: LightYellow; padding: 8px; margin-top: 8px}',
'p {background: Yellow; padding: 8px; margin-top: 8px}'
]
})
export class OnChangesComponent implements OnChanges {
// #docregion inputs
@Input() hero: Hero;
@Input() power: string;
// #enddocregion inputs
changeLog: string[] = [];
// #docregion ng-on-changes
ngOnChanges(changes: {[propertyName: string]: SimpleChange}) {
for (let propName in changes) {
let chng = changes[propName];
let cur = JSON.stringify(chng.currentValue);
let prev = JSON.stringify(chng.previousValue);
this.changeLog.push(`${propName}: currentValue = ${cur}, previousValue = ${prev}`);
}
}
// #enddocregion ng-on-changes
reset() { this.changeLog.length = 0; }
}
/***************************************/
@Component({
selector: 'on-changes-parent',
templateUrl: 'app/on-changes-parent.component.html',
styles: ['.parent {background: Lavender;}'],
directives: [OnChangesComponent]
})
export class OnChangesParentComponent {
hero: Hero;
power: string;
title = 'OnChanges';
@ViewChild(OnChangesComponent) childView: OnChangesComponent;
constructor() {
this.reset();
}
reset() {
// new Hero object every time; triggers onChanges
this.hero = new Hero('Windstorm');
// setting power only triggers onChanges if this value is different
this.power = 'sing';
if (this.childView) { this.childView.reset(); }
}
}