| 
									
										
										
										
											2016-04-27 11:28:22 -07:00
										 |  |  | /* tslint:disable:forin */ | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  | // #docregion
 | 
					
						
							| 
									
										
										
										
											2016-09-01 02:08:57 +01:00
										 |  |  | import { Component, DoCheck, Input, ViewChild } from '@angular/core'; | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class Hero { | 
					
						
							| 
									
										
										
										
											2016-04-27 11:28:22 -07:00
										 |  |  |   constructor(public name: string) {} | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @Component({ | 
					
						
							|  |  |  |   selector: 'do-check', | 
					
						
							|  |  |  |   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> | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |   </div> | 
					
						
							|  |  |  |   `,
 | 
					
						
							|  |  |  |   styles: [ | 
					
						
							|  |  |  |     '.hero {background: LightYellow; padding: 8px; margin-top: 8px}', | 
					
						
							|  |  |  |     'p {background: Yellow; padding: 8px; margin-top: 8px}' | 
					
						
							|  |  |  |   ] | 
					
						
							|  |  |  | }) | 
					
						
							| 
									
										
										
										
											2016-09-01 02:08:57 +01:00
										 |  |  | export class DoCheckComponent implements DoCheck { | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |   @Input() hero: Hero; | 
					
						
							|  |  |  |   @Input() power: string; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   changeDetected = false; | 
					
						
							| 
									
										
										
										
											2016-04-27 11:28:22 -07:00
										 |  |  |   changeLog: string[] = []; | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |   oldHeroName = ''; | 
					
						
							|  |  |  |   oldPower = ''; | 
					
						
							|  |  |  |   oldLogLength = 0; | 
					
						
							|  |  |  |   noChangeCount = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // #docregion ng-do-check
 | 
					
						
							|  |  |  |   ngDoCheck() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (this.hero.name !== this.oldHeroName) { | 
					
						
							|  |  |  |       this.changeDetected = true; | 
					
						
							| 
									
										
										
										
											2016-04-27 11:28:22 -07:00
										 |  |  |       this.changeLog.push(`DoCheck: Hero name changed to "${this.hero.name}" from "${this.oldHeroName}"`); | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |       this.oldHeroName = this.hero.name; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (this.power !== this.oldPower) { | 
					
						
							|  |  |  |       this.changeDetected = true; | 
					
						
							| 
									
										
										
										
											2016-04-27 11:28:22 -07:00
										 |  |  |       this.changeLog.push(`DoCheck: Power changed to "${this.power}" from "${this.oldPower}"`); | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |       this.oldPower = this.power; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (this.changeDetected) { | 
					
						
							|  |  |  |         this.noChangeCount = 0; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         // log that hook was called when there was no relevant change.
 | 
					
						
							| 
									
										
										
										
											2016-04-27 11:28:22 -07:00
										 |  |  |         let count = this.noChangeCount += 1; | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |         let noChangeMsg = `DoCheck called ${count}x when no change to hero or power`; | 
					
						
							|  |  |  |         if (count === 1) { | 
					
						
							|  |  |  |           // add new "no change" message
 | 
					
						
							|  |  |  |           this.changeLog.push(noChangeMsg); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |           // update last "no change" message
 | 
					
						
							| 
									
										
										
										
											2016-04-27 11:28:22 -07:00
										 |  |  |           this.changeLog[this.changeLog.length - 1] = noChangeMsg; | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     this.changeDetected = false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   // #enddocregion ng-do-check
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   reset() { | 
					
						
							|  |  |  |     this.changeDetected = true; | 
					
						
							|  |  |  |     this.changeLog.length = 0; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /***************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @Component({ | 
					
						
							| 
									
										
										
										
											2016-09-25 18:51:54 -07:00
										 |  |  |   moduleId: module.id, | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |   selector: 'do-check-parent', | 
					
						
							| 
									
										
										
										
											2016-09-25 18:51:54 -07:00
										 |  |  |   templateUrl: 'do-check-parent.component.html', | 
					
						
							| 
									
										
										
										
											2016-08-09 17:38:25 +01:00
										 |  |  |   styles: ['.parent {background: Lavender}'] | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  | }) | 
					
						
							|  |  |  | export class DoCheckParentComponent { | 
					
						
							| 
									
										
										
										
											2016-04-27 11:28:22 -07:00
										 |  |  |   hero: Hero; | 
					
						
							|  |  |  |   power: string; | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |   title = 'DoCheck'; | 
					
						
							| 
									
										
										
										
											2016-04-27 11:28:22 -07:00
										 |  |  |   @ViewChild(DoCheckComponent) childView: DoCheckComponent; | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   constructor() { this.reset(); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-27 11:28:22 -07:00
										 |  |  |   reset() { | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |     this.hero = new Hero('Windstorm'); | 
					
						
							|  |  |  |     this.power = 'sing'; | 
					
						
							| 
									
										
										
										
											2016-04-27 11:28:22 -07:00
										 |  |  |     if (this.childView) { this.childView.reset(); } | 
					
						
							| 
									
										
										
										
											2016-01-23 18:21:09 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | } |