| 
									
										
										
										
											2016-04-14 10:36:38 -07:00
										 |  |  | /* tslint:disable:forin */ | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  | // #docregion
 | 
					
						
							|  |  |  | import { | 
					
						
							| 
									
										
										
										
											2016-05-06 06:17:34 -07:00
										 |  |  |   Component, Input, OnChanges, | 
					
						
							| 
									
										
										
										
											2016-05-03 14:06:32 +02:00
										 |  |  |   SimpleChange, ViewChild | 
					
						
							| 
									
										
										
										
											2016-04-27 11:28:22 -07:00
										 |  |  | } from '@angular/core'; | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  | class Hero { | 
					
						
							| 
									
										
										
										
											2016-04-14 10:36:38 -07:00
										 |  |  |   constructor(public name: string) {} | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @Component({ | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |   selector: 'on-changes', | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  |   template: `
 | 
					
						
							|  |  |  |   <div class="hero"> | 
					
						
							|  |  |  |     <p>{{hero.name}} can {{power}}</p> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     <h4>-- Change Log --</h4> | 
					
						
							| 
									
										
										
										
											2016-04-29 01:36:35 +01:00
										 |  |  |     <div *ngFor="let chg of changeLog">{{chg}}</div> | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  |   </div> | 
					
						
							|  |  |  |   `,
 | 
					
						
							|  |  |  |   styles: [ | 
					
						
							|  |  |  |     '.hero {background: LightYellow; padding: 8px; margin-top: 8px}', | 
					
						
							|  |  |  |     'p {background: Yellow; padding: 8px; margin-top: 8px}' | 
					
						
							|  |  |  |   ] | 
					
						
							|  |  |  | }) | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  | export class OnChangesComponent implements OnChanges { | 
					
						
							|  |  |  | // #docregion inputs
 | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  |   @Input() hero: Hero; | 
					
						
							|  |  |  |   @Input() power: string; | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  | // #enddocregion inputs
 | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-14 10:36:38 -07:00
										 |  |  |   changeLog: string[] = []; | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |   // #docregion ng-on-changes
 | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  |   ngOnChanges(changes: {[propertyName: string]: SimpleChange}) { | 
					
						
							|  |  |  |     for (let propName in changes) { | 
					
						
							| 
									
										
										
										
											2016-05-06 06:17:34 -07:00
										 |  |  |       let chng = changes[propName]; | 
					
						
							|  |  |  |       let cur  = JSON.stringify(chng.currentValue); | 
					
						
							|  |  |  |       let prev = JSON.stringify(chng.previousValue); | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  |       this.changeLog.push(`${propName}: currentValue = ${cur}, previousValue = ${prev}`); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |   // #enddocregion ng-on-changes
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   reset() { this.changeLog.length = 0; } | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /***************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @Component({ | 
					
						
							|  |  |  |   selector: 'on-changes-parent', | 
					
						
							| 
									
										
										
										
											2016-04-14 10:36:38 -07:00
										 |  |  |   templateUrl: 'app/on-changes-parent.component.html', | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |   styles: ['.parent {background: Lavender;}'], | 
					
						
							|  |  |  |   directives: [OnChangesComponent] | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  | }) | 
					
						
							|  |  |  | export class OnChangesParentComponent { | 
					
						
							| 
									
										
										
										
											2016-04-14 10:36:38 -07:00
										 |  |  |   hero: Hero; | 
					
						
							|  |  |  |   power: string; | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |   title = 'OnChanges'; | 
					
						
							| 
									
										
										
										
											2016-04-27 11:28:22 -07:00
										 |  |  |   @ViewChild(OnChangesComponent) childView: OnChangesComponent; | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   constructor() { | 
					
						
							|  |  |  |     this.reset(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-27 11:28:22 -07:00
										 |  |  |   reset() { | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |     // new Hero object every time; triggers onChanges
 | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  |     this.hero = new Hero('Windstorm'); | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |     // setting power only triggers onChanges if this value is different
 | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  |     this.power = 'sing'; | 
					
						
							| 
									
										
										
										
											2016-04-14 10:36:38 -07:00
										 |  |  |     if (this.childView) { this.childView.reset(); } | 
					
						
							| 
									
										
										
										
											2015-11-21 11:23:40 -08:00
										 |  |  |   } | 
					
						
							|  |  |  | } |